0

I want to configure my site so it runs in a subdirectory rather than mydomain/. I mean instead of going to mysite.com/ and seeing the website, I want it to be seen from mydomain/myproject. I am using uwsgi to talk to a flask website and here is my /etc/nginx/sites-available/myproject config file.

server {
    server_name mydomain www.mydomain;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/root/Desktop/myproject/myproject.sock;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mydomain/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mydomain/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


}
server {
    if ($host = www.mydomain) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = mydomain) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name mydomain www.mydomain;
    return 404; # managed by Certbot

}

I tried to change the code from location / to location /myproject or location = /myproject but it gave me not found!

Added information Here is my config.ini file

[uwsgi]
module = server:app

master = true
processes = 5

socket = myproject.sock
chmod-socket = 660
vacuum = true

die-on-term = true

route-run = fixpathinfo:

I am using uwsgi version 2.0.18 and nginx/1.14.0 (Ubuntu) Thanks

FreeMind
  • 213
  • 3
  • 20

1 Answers1

1

try this...

NGINX config:


location /mylocation {
        include uwsgi_params;
        uwsgi_pass unix:/myproject/myproject.sock;
        uwsgi_param SCRIPT_NAME /mylocation;
    }

uwsgi ini file:

route-run = fixpathinfo:

Edit: i tried it with rewriting the path in my nginx config and it worked.. nothing special to configure in your wsgi ini file!

location /be {
    rewrite /be/(.+) /$1 break;
        include uwsgi_params;
        uwsgi_pass unix:/myproject/myproject.sock;

    }

EDIT: So, my conclusion is, fixpathinfo in uwsgi ini seems not to work when using "/" (root) inside FlaskApp. If Flask is using "/", you should set rewrite /be/(.*) /$1 break; in your NGINX Config

Thank you CyberDem0n!! Nginx - Rewrite the request_uri before uwsgi_pass

tgyger
  • 231
  • 2
  • 7
  • what is `fixpathinfo`? – FreeMind Nov 08 '19 at 22:51
  • it still gives `not found` – FreeMind Nov 08 '19 at 23:01
  • This action allows you to set SCRIPT_NAME in nginx without bothering to rewrite the PATH_INFO (something nginx cannot afford) https://uwsgi-docs.readthedocs.io/en/latest/Changelog-2.0.11.html – tgyger Nov 09 '19 at 08:08
  • have you tried to configure a mountpoint in your ini file? like mount = /app1=app1.py? – tgyger Nov 09 '19 at 08:12
  • stupid question... but have you restarted your nginx AND app service after changing nginx config and ini file? which version of uwsgi/nginx do you have? – tgyger Nov 09 '19 at 10:46
  • also check out: https://stackoverflow.com/questions/12054001/nginx-rewrite-the-request-uri-before-uwsgi-pass – tgyger Nov 09 '19 at 10:51
  • I added extra info about `config.ini` file. In addition I restart nginx everytime I make changes, so yes! – FreeMind Nov 10 '19 at 03:34
  • so strange... it should work. you also did "systemctl restart myproject" (myproject.service socket)? see my edit... have you also tried the second solution (rewrite path in nginx with "rewrite /myproject/(.+) /$1 break;"? – tgyger Nov 10 '19 at 06:59
  • Yes I did restart both socket and nginx. I do it everytime for every change and of course I did change `rewrite` path as you stated but I get not found. Do I have to configure flask router? – FreeMind Nov 11 '19 at 00:48
  • 1
    aaahh... i think i know where it is... i think you are using "/" (root) in your flaskapp right? so change "rewrite /myproject/(.+) /$1 break;" to "rewrite /myproject/ / break;" – tgyger Nov 11 '19 at 08:43
  • please can you set "rewrite_log on;" in your nginx server config and then post the log in /var/log/nginx/error.log ? – tgyger Nov 11 '19 at 08:46
  • Dude `rewrite /myproject/ / break;` solved the problem and yes I was using `/` in my flask app! ‌But the problem is now my app gives not found and sending ajax requests results in not found error and everything is not working. – FreeMind Nov 11 '19 at 10:29
  • so, try ```rewrite /myproject/(.*) /$1 break;``` – tgyger Nov 11 '19 at 11:43