0

I currently have a Djago app running on my main site when I visit mysite.com. However, I'd like mysite.com/flaskapp to run a separate Flask application. I'm able to set up two nginx site-enabled config files and run each app on a different port but, for various reasons, I'd like to run them all on the same port (if possible). When I configure my flaskapp/ location in my nginx server file I get a 404 error.

Here's my supervisor config file:

[program:MYSITE]
command=/var/www/html/MYSITE/prodenv/bin/gunicorn --workers 3 --bind unix:/var/www/html/MYSITE/public_html/MYSITE.sock MYSITE.wsgi
directory=/var/www/html/MYSITE/public_html
autostart=true
autorestart=true
stderr_logfile=/var/log/MYSITE.err.log
stdout_logfile=/var/log/MYSITE.out.log


[program:FLASKAPP]
directory=/var/www/html/MYSITE/public_html/FLASKAPP/api
command=/var/www/html/MYSITE/public_html/FLASKAPP/venv/bin/gunicorn --workers 3 --bind unix:/var/www/html/MYSITE/public_html/FLASKAPP/api/FLASKAPP.sock FLASKAPP:app
autostart=true
autorestart=true
stderr_logfile=/var/log/FLASKAPP.err.log
stdout_logfile=/var/log/FLASKAPP.out.log

And my nginx site-enabled file:

server {
    listen 80;
    listen [::]:80;

    server_name MYSITE;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
            root /var/www/html/MYSITE/public_html;
        expires 30d;
        }

    location / {
            include proxy_params;
            proxy_pass http://unix:/var/www/html/MYSITE/public_html/MYSITE.sock;
        }

    location /FLASKAPP/ {
        include proxy_params;
        proxy_pass http://unix:/var/www/html/MYSITE/public_html/FLASKAPP/api/FLASKAPP.sock; 
        }


}

Any ideas? Thanks!

  • Check the ownership of the socket files, they need to be owned by your nginx user. – Simon Greenwood Dec 14 '17 at 7:07
  • @SimonGreenwood I don't think that's the problem because if I split FLASKAPP into it's own nginx config, just with a different port, it works. – pnus Dec 14 '17 at 7:09
0

There is typo in proxy_path. You have proxy_pass http://unix:/var/www/html/MYSITE/public_html/FLASKAPP/api/FLASKAPP.sock;, but there should be fastcgi_pass unix:/var/www/html/MYSITE/public_html/FLASKAPP/api/FLASKAPP.sock;. Remove http://, change proxy_pass to fastcgi_pass and everything should work.

  • Unfortunately, this didn't work. Removing the http:// wouldn't even allow me to restart my nginx service. I received a "Failed to start A high performance web server and a reverse proxy server." error in the log. – pnus Dec 14 '17 at 14:21
  • @pnus sorry, there should be fastcgi_pass instead of proxy_pass. – Alexander Tolkachev Dec 14 '17 at 14:24
  • Well, I'm not getting a 404 error like I was before but now it's just spinning it's wheels until it eventually gets a 502 error :-/ – pnus Dec 15 '17 at 4:00
0

I figured it out. I rewrote the url to remove the subdirectory and now it all works:

      rewrite ^/FLASKAPP(.*)$ $1 break;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.