Run multiple applications within the same virtualhost.
If you access http://example.com/app1/hello, you can jump to / hello of the app running on uwsgi on localhost.
Understand three important variables (probably from CGI).
REQUEST_URI: /app/hello
SCRIPT_NAME: /app
PATH_INFO: /hello
REQUEST_URI is the path after the host name accessed by the user.
SCRIPT_NAME is where the app is running. CGI has an extension such as .cgi
or php has an extension such as .php
.
PATH_INFO is the path after SCRIPT_NAME. The app's URL dispatcher will see this variable.
If these three variables are not set properly, URL dispatch and redirect will not work properly.
nginx + uwsgi
Uwsgi_params, which is provided by default and can be used just by including,
ʻUwsgi_param REQUEST_URI $ request_uriis written as it is, there is no need to change it. The
PATH_INFO is set to
$ document_uri`, but you need to overwrite it appropriately.
There are two ways to set SCRIPT_NAME and PATH_INFO, one is to do it on the nginx side and the other is to do it on the uwsgi side.
This method is recommended because it can be applied not only to uwsgi but also to other reverse proxy methods such as http and fastcgi.
nginx.conf
location ~ ^/app/(.*)$ {
uwsgi_pass unix:/var/run/uwsgi/app.sock;
include /usr/local/nginx/conf/uwsgi_params;
uwsgi_param SCRIPT_NAME /app;
uwsgi_param PATH_INFO /$1;
}
If you used uwsgi's mount to set your application's prefix, you can also use the manage-script-name
option to set SCRIPT_NAME and PATH_INFO.
This method is convenient if you are using mount, for example, the application itself and the wsgi script for health check are prepared separately.
nginx.conf
location ~ ^/app/(.*)$ {
uwsgi_pass unix:/var/run/uwsgi/app.sock;
include /usr/local/nginx/conf/uwsgi_params;
}
uwsgi.ini
[uwsgi]
master = true
lazy-apps = true
socket = /var/run/uwsgi/app.sock
workers = 4
mount = /app=wsgi.py
manage-script-name = true
Recommended Posts