Theresa Ott
Theresa Ott

Reputation: 51

I can't access Flask website served by gunicorn when running nginx

I apologize in advance, I am quite new to all of this!

I have written an application using Flask-socketio. The website worked fine on the developement server and accessing it via localhost:5000.

I am serving the application using gunicorn and when I run

gunicorn --bind 0.0.0.0:5000 wsgi:app

I can access the website via 0.0.0.0:5000.

Now I tried using systemd and nginx to create an accesspoint to the outside world. The nginx is working without an error but if I try to open it in a browser it is not working anymore. I have tried all kinds of things, using my server's IP address and trying 0.0.0.0 again.

I am calling it by starting nginx with systemd. Maybe that is wrong?

systemctl status nginx

I am at a loss here, no solution suggested online has helped me so far.

My application structure is: /opt/myproj/proj_srv/functions (like application.py and wsgi.py) /opt/myproj/proj_srv/functions/static (all static files like index.html)

My project's config file at /etc/nginx/sites-enabled/NEMO

        listen 80;
        server_name FRTM999.com www.FRTM999.com;
        location/ {
                proxy_pass http://127.0.0.1:8080;
                }
        location /static/ {
                autoindex on;
        alias /opt/NEMO/NEMO_Srv/functions/static/;
          }
}

the nginx config file /etc/nginx/nginx.conf


events {

    worker_connections 1024;

}

http {

    sendfile on;

    gzip              on;
    gzip_http_version 1.0;
    gzip_proxied      any;
    gzip_min_length   500;
    gzip_disable      "MSIE [1-6]\.";
    gzip_types        text/plain text/xml text/css
                      text/comma-separated-values
                      text/javascript
                      pplication/x-javascript
                      application/atom+xml;

    # Configuration containing list of application servers
    upstream app_servers {

        server 127.0.0.1:8080;
        # server 127.0.0.1:8081;
        # ..
}

    # Configuration for Nginx
    server {

        # Running port
        listen 80;

        # Settings to serve static files 
        location ^~ /static/  {

            # Example:
            # root /full/path/to/application/static/file/dir;
            root /opt/NEMO/NEMO_Srv/functions/static/;

        }
 Serve a static file (ex. favico)
        # outside /static directory
        location = /favico.ico  {

            root /opt/NEMO/NEMO_Srv/functions/static/favico.ico;

        }

        # Proxy connections to the application servers
        # app_servers
        location / {

            proxy_pass         http://app_servers;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
      }
    }
}

My systemd service file

[Unit]
Description=uWSGI instance to serve myproject
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/opt/NEMO/NEMO_Srv/functions
ExecStart=/usr/bin/env gunicorn wsgi:app -b 0.0.0.0:5000

[Install]
WantedBy=multi-user.target

I am not sure what else to provide, please let me know if I can make my question clearer. Also thanks already for taking the time! I appreciate it immensely!

Upvotes: 1

Views: 2331

Answers (1)

Jophy Ye
Jophy Ye

Reputation: 116

Looking at your code, it seems that nginx is just proxying to port 8080.

upstream app_servers {
    server 127.0.0.1:8080;
    # server 127.0.0.1:8081;
}

location / {
    proxy_pass http://127.0.0.1:8080;
}

However, gunicorn seems to be listening on port 5000.

ExecStart=/usr/bin/env gunicorn wsgi:app -b 0.0.0.0:5000

The two applications are not communicating on the same port, so I think that's the reason to your problem.

Upvotes: 1

Related Questions