I have a Nginx vhost than is configured as such:
...
location /one {
include uwsgi_params;
uwsgi_pass unix:///.../one.sock;
}
location /two {
include uwsgi_params;
uwsgi_pass unix:///.../two.sock
}
...
This is a simplified configuration of course
When I request /one/something I would like my Python script to receive /something as request_uri.
I'm using BottlePy but would like this to be handled by Nginx and not in my Python code.
Can I do something like uwsgi_param REQUEST_URI replace($request_uri, '^/one', '')?
Edit
Here is the request from my Python code:
[pid: 30052|app: 0|req: 1/1] () {42 vars in 844 bytes} [Tue Aug 21 14:22:07 2012] GET /one/something => generated 0 bytes in 4 msecs (HTTP/1.1 200) 2 headers in 85 bytes (0 switches on core 0)
So Python is OK but uWSGI is not.
How to fix that?
location /one {
rewrite /one/(.+) /$1 break;
include uwsgi_params;
uwsgi_pass unix:///.../one.sock;
}
I know this thread is old, but there is another way to solve this if you are using uWSGI to run your python app.
[uwsgi]
route-uri = ^/one/(.*) rewrite:/$1
I just met the same problem, and here is a solution
location /one {
include uwsgi_params;
uwsgi_pass unix:///.../one.sock;
uwsgi_param SCRIPT_NAME /one;
uwsgi_modifier1 30;
}
You can found more about uwsgi_modifier1 here:
http://uwsgi-docs.readthedocs.org/en/latest/Nginx.html#hosting-multiple-apps-in-the-same-process-aka-managing-script-name-and-path-info
I've solved that in another way:
[uwsgi]
module = wsgi:application
master = true
processes = 10
socket = 127.0.0.1:9090
mount = /one=customscript.py
manage-script-name = true
nginx
location /one {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090;
}
Related
I am trying to test-run a django project on my VPS for the first time. I followed a step-by-step tutorial on a blog (thanks to a nice guy on #django channel at liberachat). The setup involves uWSGI and nginx. The django project files are at /srv/www/site/*. uwsgi configuration is at /etc/uwsgi.d/mysite.com.ini, and nginx configuration is at /etc/nginx/conf.d/mysite.com.conf.
Here is what happens when I start uwsgi (systemctl start uwsgi):
Jul 29 14:35:09 mysite.com uwsgi[6998]: [uWSGI] getting INI configuration from mysite.com.ini
Jul 29 14:35:09 mysite.com uwsgi[6318]: Fri Jul 29 14:35:09 2022 - [emperor] curse the uwsgi instance mysite.com.ini (pid: 6998)
Jul 29 14:35:09 mysite.com uwsgi[6318]: Fri Jul 29 14:35:09 2022 - [emperor] removed uwsgi instance mysite.com.ini
How do I interpret and fix that?
contents of /etc/uwsgi.d/mysite.com.ini:
procname-master = demosite
# Now paths can be specified relative to here.
chdir = /srv/www/
# allow nginx
uid = 981
gid = 981
chmod-socket = 644
socket = server.sock
# Task management
; Max 4 processes
processes = 2
; Each running 4 threads
threads = 2
; Reduce to 1 process when quiet
cheaper = 1
; Save some memory per thread
thread-stack-size = 512
# Logging
plugin = logfile
; Log request details here
req-logger = file:logs/request.log
; Log other details here
logger = file:logs/error.log
log-x-forwarded-for = true
# Python app
plugin = python3
; Activate this virtualenv
virtualenv = venv/
; Add this dir to PYTHONPATH so Python can find our code
pythonpath = site/
; The WSGI module to load
module = mysite.wsgi
# Don't load the app in the Master - saves memory in quiet times
lazy-apps = true
contents of /etc/nginx/conf.d/mysite.conf:
# Allow gzip compression
gzip_types text/css application/json application/x-javascript;
gzip_comp_level 6;
gzip_proxied any;
# Look for files with .gz to serve pre-compressed data
gzip_static on;
server {
listen 80;
# The hostname(s) of my site
server_name mysite.com;
# Where to look for content (static and media)
root /srv/www/html/;
# Defines the connection for talking to our Django app service
location #proxy {
# Pass other requests to uWSGI
uwsgi_pass unix://srv/www/server.sock;
include uwsgi_params;
}
# nginx docs recommend try_files over "if"
location / {
# Try to serve existing files first
#try_files $uri #proxy =404;
root /srv/www/html/;
}
}
What did I miss?
I am trying to serve a Django 3 build from nginx through wsgi and I cannot seem to get the last stage. I can browse to the Django site by a runserver and launching the uwsgi.ini, but, when I try to browse to the site through nginx I get a 502 error (bad gateway)(firefox). If I try from a remote site it renders the nginx home page.
I have a build of Apache on the same server. I had to spoof an IP and use a unique port to get them running side by side.
the nginx/error.log does not register any problem.
Below is the uwsgi.ini.
[uwsgi]
# variables
projectname = website
base = /opt/website/
# configuration
master = true
http = :8000
uid = nginx
virtualenv = /opt/website/djangoprojectenv
pythonpath = %(base)
chdir = %(base)
env = DJANGO_SETTINGS_MODULE=%(projectname).settings.pro
#module = %(projectname).wsgi:application
module = website.wsgi:application
socket = /tmp/%(projectname).new.sock
chown-socket = %(uid):nginx
chmod-socket = 666
And below is the conf file from nginx/conf.d
server {
listen 192.168.1.220:81;
server_name willdoit.com;
access_log off;
error_log /var/log/nginx_error.log;
location / {
uwsgi_pass unix:/tmp/website.sock;
include /etc/nginx/uwsgi_params;
uwsgi_read_timeout 300s;
client_max_body_size 32m;
}
}
The /tmp/website.sock file is owned by nginx:nginx.
If there are additional details I need to post, please advise. Any help you can provide would be welcome.
The socket was defined incorrectly. Closing.
I'm teaching myself how to setup an Ubuntu Server to run my Django application. I want to use Nginx + uwsgi. I know that this question can be very easy for experts but I've spent 6 days looking for it over the internet without getting it (in any case, forgive me if there is any link with the answer). I've followed a lot of tutorials and posts but I didn't found a solution.
I describe my file structure below:
My django project is located in /usr/local/projects/myproject
My virtualenv is in /root/.virtualenvs/myproject
My uwsgi config file myproject.ini is in /etc/uwsgi/apps-available/ and correctly symbolic linked in /etc/uwsgi/apps-enabled/
[uwsgi]
plugins = python
socket = /tmp/myproject.sock
chmod-socket = 644
uid = www-data
gid = www-data
master = true
enable-threads = true
processes = 2
no-site=true
virtualenv = /root/.virtualenvs/myproject
chdir = /usr/local/projects/myproject
module = myproject.wsgi:application
pidfile = /usr/local/projects/myproject/myproject.pid
logto = /var/log/uwsgi/myproject_uwsgi.log
vacuum = true
My nginx config file myproject.conf is in /etc/nginx/sites-available/ and correctly symbolic linked in /etc/nginx/sites-enabled/
# the upstream component nginx needs to connect to
upstream django {
server unix:///tmp/myproject.sock; # for a file socket
}
server {
listen 80;
server_name dev.myproject.com www.dev.myproject.com;
access_log /var/log/nginx/myproject_access.log;
error_log /var/log/nginx/myproject_error.log;
location / {
uwsgi_pass unix:///tmp/myproject.sock;
include /etc/nginx/uwsgi_params;
uwsgi_param UWSGI_SCRIPT myproject.wsgi;
}
location /media/ {
alias /usr/local/projects/myproject/media/;
}
location /static/ {
alias /usr/local/projects/myproject/static/;
}
}
When I try to access to dev.myproject.com I get an Internal Server Error. Then I take a look to my uwsgi log:
Traceback (most recent call last):
File "./myproject/wsgi.py", line 9, in <module>
import os
ImportError: No module named os
Sat Jul 26 17:39:16 2014 - unable to load app 0 (mountpoint='') (callable not found or import error)
Sat Jul 26 17:39:16 2014 - --- no python application found, check your startup logs for errors ---
[pid: 8559|app: -1|req: -1/8] 79.148.138.10 () {40 vars in 685 bytes} [Sat Jul 26 17:39:16 2014] GET / => generated 21 bytes in 0 msecs (HTTP/1.1 500) 1 headers in 57 bytes (0 switches on core 0)
I need your help because I'm not able to find a solution despite the posibility of being very simple.
If you need to know something else let me know and I will update my question as soon as possible.
I finally found a solution.
I followed kchan's suggestion about not putting any of the contents in the /root/ directory. Basically, I did some small changes in my myproject.conf file and my myproject.ini file. I created a user and structured everything like below:
uwsgi config file myproject.ini in /etc/uwsgi/apps-available/ and correctly symbolic linked in /etc/uwsgi/apps-enabled/
[uwsgi]
plugins = python
socket = /tmp/myproject.sock
chmod-socket = 644
uid = www-data
gid = www-data
master = true
enable-threads = true
processes = 2
virtualenv = /home/user/.virtualenvs/myproject
chdir = /home/user/projects/myproject
module = myproject.wsgi:application
pidfile = /home/user/projects/myproject/myproject.pid
daemonize = /var/log/uwsgi/myproject_uwsgi.log
vacuum = true
nginx config file myproject.conf in /etc/nginx/sites-available/ and correctly symbolic linked in /etc/nginx/sites-enabled/
# the upstream component nginx needs to connect to
upstream django {
server unix:///tmp/myproject.sock; # for a file socket
}
server {
listen 80;
server_name dev.myproject.com www.dev.myproject.com;
access_log /var/log/nginx/myproject_access.log;
error_log /var/log/nginx/myproject_error.log;
location / {
uwsgi_pass django;
include /etc/nginx/uwsgi_params;
}
location /media/ {
alias /home/user/projects/myproject/media/;
}
location /static/ {
alias /home/user/projects/myproject/static/;
}
}
I must say that I think the real problem was to try to setup my DB configuration in the postactivate file of my virtualenv. Hope to help someone else.
I'm new to both, I got to run 2 Django skeleton apps (just shows the "It works!" page) using Emperor, but I want to try it without Emperor. (to better understand how it works)
My nginx.conf:
# snipped...
server {
listen 92;
server_name example.com;
access_log /home/john/www/example.com/logs/access.log;
error_log /home/john/www/example.com/logs/error.log;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8001;
}
}
# snipped...
And I start uWSGI by:
$ uwsgi --ini /home/john/www/example.com/uwsgi.ini
With uwsgi.ini being:
[uwsgi]
http = :8001
chdir = /home/john/www/example.com/example
module = example.wsgi
master = True
home = /home/john/Envs/example.com
Once uwsgi and nginx are running, I can access localhost:8001, but not localhost:92.
What am I missing?
Thanks in advance.
You are telling the uwsgi process to serve applications using the http protocol. This feature is meant mainly for developer convenience. You should instead tell it to use the uwsgi protocol:
[uwsgi]
protocol = uwsgi
socket = 127.0.0.1:8001
chdir = /home/john/www/example.com/example
module = example.wsgi
master = True
home = /home/john/Envs/example.com
I have deployed a service.
It is served by nginx, uwsgi, django, pgpool, postgresql stack.
The page that has no db access is no problem.
However, the page that has some data from postgres is tpo slow.
Actually, db query time is quite fast. under 10 ms.
But result to client is over the 120000 ms
uwsgi log
[pid: 2056|app: 0|req: 4/10] 211.207.245.120 () {44 vars in 1116 bytes} [Thu Jul 19 00:53:31 2012] GET /account/admin/cb_main/invitationuser/ => generated 38606 bytes in 122126 msecs (HTTP/1.1 200) 8 headers in 373 bytes (1 switches on core 0)
My settings are below
Please check my settings and solve my problems
nginx settings
upstream cuying {
ip_hash;
server 127.0.0.1:9001;
}
server {
listen 8080;
root /home/cuying_mgr/;
client_max_body_size 20M;
server_name cuying.com;
location / {
uwsgi_pass cuying;
include uwsgi_params;
uwsgi_read_timeout 120;
uwsgi_send_timeout 120;
}
}
uwsgi settings
chdir=/home/cuying_mgr/develop/virenv/cuying/cuying
processes=2
workers=8
enable-threads=true
socket=127.0.0.1:9001
module=cuyingProject.wsgi:application
master=True
pidfile=/tmp/cuying-master.pid
vacuum=True
close-on-exec=True
max-requests=3000
post-buffering=8192
socket-timeout=120
limit-post=20480000
virtualenv=/home/cuying_mgr/develop/virenv/cuying
daemonize=/var/log/uwsgi/cuying.log
Please help me out!
It's too slow.....
Use profiler to find out the slowest point while processing request. It looks like the problem was not caused by postgresql or uwsgi things.
See ProfilingDjango wiki page.