Home > Back-end >  Running a proxy_pass on a subpath in NGINX
Running a proxy_pass on a subpath in NGINX

Time:10-26

I am trying to run a Flask app on Gunicorn through an Nginx server. I would like the app to run on a sub-directory instead of through a different port, if possible, but all I get are 404 errors. Here is my conf file, which is an included file in the conf.d folder:

server {
    listen          80;
    server_name     127.0.0.1;
    location / {
        root /var/www/html;
    }
    location /chess/ {
        proxy_pass http://unix:/usr/share/nginx/sockets/chess.sock;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Could someone please tell me how to do this? I have looked all over and tried a lot of different things, to no avail. It runs fine on a different port, but that is not what I want. A subdomain is also a suitable option, but I can only get that to work in production, not development, for some reason. Someone posed the question project directory

myproject.py:

from flask import Flask
app = Flask(__name__)

@app.route("/chess/",defaults={'name': None})
@app.route("/chess/<name>")
def hello(name):
    if name is None:
        name="!"
    else: 
        name = ", "   name
    # PLEASE don't use the following line of code in a real app, it creates a self-xss vulnerability. ALWAYS sanitize user input. 
    return f"<h1 style='color:blue'>Hello There{name}</h1>"

if __name__ == "__main__":
    app.run(host='0.0.0.0')

nginx - /etc/nginx/sites-enabled/myproject (symlink)

server {
    listen 8080;
    server_name your_domain www.your_domain;


    location / {
        root /home/username/myproject/static/;
    }



    location /chess/ {
        include proxy_params;
        proxy_pass http://unix:/home/username/myproject/myproject.sock;
    }
}

<host>:8080/chess/stackoverflow:

flask-app

<host>:8080/a.html: (actually served from /home/myproject/static)

static-file

generally and for future reference - try looking at nginx logs (/var/log/nginx) or service logs (journalctl -u myproject or systemctl status myproject)

CodePudding user response:

Further research revealed that it is not recommended to run Gunicorn on port 80, anyway, but rather you should run another proxy_pass on NGINX like so:

server {
    listen  localhost:80;
    location / {
        root /var/www/html;
    }
    location /chess/ {
        proxy_pass http://localhost:8080/;
    }
}

server {
    listen  localhost:8080;

    location / {
        proxy_pass http://unix:/usr/share/nginx/sockets/chess.sock;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

It is important to note that the last slash in the proxy_pass http://localhost:8080/ line is necessary to avoid a 404 error. No changes to the Flask app code were necessary. With this setup, both localhost/chess and localhost:8080 directed to the Flask app, while localhost directed to the original HTML root.

  • Related