Search code examples
pythonnginxflaskdeploymentuwsgi

How to redirect a Flask application with Nginx and uWSGI?


When the user enters the url http://localhost:8000/Hello, I need to redirect him to http://localhost:8081/Hello.

Here is my attempt on the deploy.conf file:

server {
  listen 8000;

  location /Hello { 
    include uwsgi_params; 
    uwsgi_pass localhost:8081/Hello; 
    }
  }

But when I check nginx with: sudo nginx -t

I get this error: nginx: [emerg] invalid host in upstream "localhost:8081/Hello" in /projects/challenge/deploy.conf

Please any help?


Solution

  • The error is due to the improper format of the uwsgi_pass directive. The uwsgi_pass directive expects a URL or a Unix socket path as its argument, not a complete URL with a path.

    Try this way:

    server {
        listen 8000;
    
        location /Hello {
            include uwsgi_params;
            uwsgi_pass localhost:8081;  # Use only the base URL
        }
    }
    

    In this configuration, you're instructing Nginx to proxy requests that match the /Hello path to the uWSGI server running on localhost:8081.

    Keep in mind that this Nginx configuration assumes that your uWSGI server is properly configured and running on localhost:8081.

    After making this change, you should reload Nginx for the changes to take effect:

    sudo systemctl reload nginx