0

I am trying to setup an nginx server to redirect to differents ports in my docker app depending on the endpoint in the URL.

I have tried changing the myapp.conf file to redirect as explained in some other questions of stackoverflow but I am not managing.

This is myapp.conf file:

server {
    listen 80;
    server_name automl.ddns.net;
    # This just redirects if you go through http
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name automl.ddns.net;
    resolver 127.0.0.11 valid=10s;
    resolver_timeout 5s;
    ssl_certificate /etc/letsencrypt/live/automl.ddns.net/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/automl.ddns.net/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
    
    # Redirect any HTTP requests to HTTPS
    if ($scheme != "https") {
        return 301 https://$server_name$request_uri;
    }

    # Proxy requests to the Flask app running on port 5000
    location /docs {
        proxy_pass http://python-fastapi:8000/docs;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        break;
    }
    location ~ ^/(.*)$ {
        client_max_body_size 20M;  # Set maximum request size to 20MB
        proxy_pass http://python-flask:5000/$request_uri;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Debuggin I notice that when trying to access /docs I am going directly through the flask block and as it has no endpoint for docs it returns nothing.

I may guess that as my last block just expects any location it may be passing through both and causing issues, however, break should avoid that.

Chris
  • 18,724
  • 6
  • 46
  • 80
  • 1
    Regular expressions have higher priority in determining a match than path based matches. See also https://stackoverflow.com/questions/5238377/nginx-location-priority – Nick ODell Jun 01 '23 at 16:10
  • 2
    What you really want for your flask location directive is `location / {` – Nick ODell Jun 01 '23 at 16:10

0 Answers0