3

When a user visits www.website.com I would like to server content as if the user went to www.website.com/frontend/. However, I want to mask the /frontend/ part of the url so the user doesn't see it. Can this be done?

What would my rewrite rule look like?

SOLVED:

location = / {
    rewrite ^/$ /frontend/ last;
}

My issue was

location = / {} #Matches the path project.example.com only (mind there is a =)
location / {} #Matches every path (mind: there is no =)
superdee
  • 637
  • 10
  • 23
  • All URLs `/some/path` should go to `/frontend/some/path`? – Ivan Shatsky Dec 15 '18 at 00:46
  • It should serve the content from my django app as if the user went to /frontend/some/path, correct. But I don't want the user to see /frontend/ in the url, just received the content from /frontend/ app – superdee Dec 15 '18 at 00:49
  • Why don't you just include frontend scripts into main page? Why not to use `location = / { ... root /frontend}` ? – Ivan Starostin Dec 15 '18 at 09:01

3 Answers3

3

I don't think using rewrite is the perfect solution (by the way i don't think it will cover all aspects of your problem and may cause new problems)

The solution is as follow:

nginx config should be something like this:

upstream django {
    server unix://tmp/gunicorn.sock;  //
}

server {
    listen 80;
    server_name <your_app_domain_here>;
    location /frontend {
        include uwsgi_params;
        proxy_pass http://django/;
    }
}

or if you are not using sock file, you can use http method. for example if you are running django on your localhost with port 8000, then change it to:

proxy_pass http://localhost:8000/;

But remember you should add this in your django settings.py. Unless it doesn't work at all:

USE_X_FORWARDED_HOST = True
FORCE_SCRIPT_NAME = "/frontend"

By this method, you are changing base url in django. so all django urls should start with fronted label. Now nginx can perfectly act as a reverse proxy for your site :)

Reza Torkaman Ahmadi
  • 2,958
  • 2
  • 20
  • 43
0

SOLVED:

location = / {
    rewrite ^/$ /frontend/ last;
}

My issue was

location = / {} #Matches the path project.example.com only (mind there is a =)
location / {} #Matches every path (mind: there is no =)
superdee
  • 637
  • 10
  • 23
0

You don't need rewrite rules for this. Just use

location / {
    proxy_pass http://<your_backend>/frontend/;
}
Ivan Shatsky
  • 13,267
  • 2
  • 21
  • 37
  • That doesn't do anything, unfortunately. `proxy_pass http://127.0.0.1:80/frontend/`; just returns "Url not found" – superdee Dec 15 '18 at 01:02
  • You forgot a trailing slash or this is just a typo? – Ivan Shatsky Dec 15 '18 at 01:02
  • Do you remove you rewriting rules before testing this? – Ivan Shatsky Dec 15 '18 at 01:05
  • Ya I did. So the only way / works is having `rewrite ^/$ /frontend/ last;`. But now both / works and /frontend/ works, making any link on the page turn the url back into /frontend/ – superdee Dec 15 '18 at 01:10
  • That is very strange. Your backend defined via IP address or via unix socket? – Ivan Shatsky Dec 15 '18 at 01:15
  • Also: do you remove `location = / { ...}` block before testing this? – Ivan Shatsky Dec 15 '18 at 01:18
  • I believe IP, but I use uwsgi so maybe unix socket? I'm not sure to be honest. And yeah removed the location block – superdee Dec 15 '18 at 01:20
  • I'm sorry, one more question: in your config what directive do you use for passing request to backend? `proxy_pass` or `uwsgi_pass`? – Ivan Shatsky Dec 15 '18 at 01:31
  • location / { uwsgi_pass django; include /etc/nginx/uwsgi_params; } – superdee Dec 15 '18 at 01:37
  • Maybe that is the reason my solution doesn't work. Accroding to documentation [`uwsgi_pass`](http://nginx.org/en/docs/http/ngx_http_uwsgi_module.html#uwsgi_pass), in contrast of [`proxy_pass`](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass), does not allow to specify an URI with the backend. In [this](https://stackoverflow.com/questions/53671687/nginx-multiple-django-apps-same-domain-different-urls) question `proxy_pass` with specified URI works fine. – Ivan Shatsky Dec 15 '18 at 01:59