[PYTHON] Setting to run application in subdirectory with nginx + uwsgi

Thing you want to do

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.

Basic knowledge

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. ThePATH_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.

Set on the nginx 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;
}

Set on the uwsgi side

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

Setting to run application in subdirectory with nginx + uwsgi
How to run tests in bulk with Python unittest
To run gym_torcs with ubutnu16
[TensorFlow 2 / Keras] How to run learning with CTC Loss in Keras
Learning history to participate in team application development with Python ~ Build Docker / Django / Nginx / MariaDB environment ~
I want to run Rails with rails s even in vagrant environment
run uwsgi server in uwsgi-gevent mode
How to run TensorFlow 1.0 code in 2.0
How to log in to Docker + NGINX
Try logging in to qiita with Python
How to work with BigQuery in Python
Create Python + uWSGI + Nginx environment with Docker
Run a Python web application with Docker
To work with timestamp stations in Python
Run (legacy) Windows apps with Windows Application Driver
How to display in the entire window when setting the background image with tkinter
How to deal with memory leaks in matplotlib.pyplot
A memorandum to make WebDAV only with nginx
[REAPER] How to play with Reascript in Python
Convert PDFs to images in bulk with Python
Type Python scripts to run in QGIS Processing
I tried to integrate with Keras in TFv1.1
How to run some script regularly in Django
How to deal with run-time errors in subprocess.call
Hello World with nginx + uwsgi + python on EC2
Steps to develop a web application in Python
Create Nginx + uWSGI + Python (Django) environment with docker
Log in to Yahoo Business with Selenium Python
Easily log in to AWS with multiple accounts
How to use tkinter with python in pyenv
How to run Leap Motion in non-Apple Python
Run the output code with tkinter, saying "A, pretending to be B" in python
From installing Flask on CentOS to making it a service with Nginx and uWSGI
How to use Docker to containerize your application and how to use Docker Compose to run your application in a development environment
What to do if you run python in IntelliJ and end with an error