Score:0

Change Nginx proxy pass public path

in flag

I have a Python/Django API with a unique endpoint /videos running on my Debian server.

The Nginx vhost looks like this:

server {

    server_name example.com;

    location / {
        # Pass to Uvicorn/Gunicorn web server service
        proxy_pass http://upstream_name/;
        proxy_set_header Host $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;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /path_to/fullchain.pem; # managed by Certbot
    ssl_certificate_key /path_to/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

upstream upstream_name {
    server 127.0.0.1:8002;
}

Thus, it successfully serve the app and its unique endpoint on https://example.com/videos.

Now, I would like to serve the app on https://example.com/my_app/videos, in order to have in the future a other apps served on the same domain/vhost (with different internal ports, different upstreams in the vhost of course).

I've been reading several similar Q/A on ServerFault, and have been trying changing location / to location /my_app, while trying different trailing slashes configs on location and proxy_pass, with no success. What am I missing here?

EDIT: More precisely:

  • With the vhost changed to location /myapp -> https://example.com/my_app/videos displays a Not Found error (not from Nginx)

  • With the vhost changed to location /my_app/ -> https://example.com/my_app/videos get redirected to https://example.com/videos/ and displays a 404 Not Found error (from Nginx)

us flag
Please show the exact configuration you tried, and what steps you took to reproduce the issue?
in flag
@TeroKilkanen Sure, edited.
us flag
You need to configure the new root URL in your application. To me, the second failure looks like your application sends the redirect to `/videos` URI.
in flag
You're right, it needs a config on app's side. I solved it by adding FORCE_SCRIPT_NAME param on Django's setting. More precisely, it needs FORCE_SCRIPT_NAME = '/my_app/' WITH the trailing slash.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.