[英]How to deny access to root but allow subdirectory access in nginx / django / python?
[英]How to serve subdirectory as root in nginx using django?
當用戶訪問www.website.com時,我希望像訪問www.website.com/frontend/一樣對內容進行服務器處理。 但是,我想屏蔽URL的/ frontend /部分,以便用戶看不到它。 能做到嗎?
我的重寫規則是什么樣的?
解決了:
location = / {
rewrite ^/$ /frontend/ last;
}
我的問題是
location = / {} #Matches the path project.example.com only (mind there is a =)
location / {} #Matches every path (mind: there is no =)
解決了:
location = / {
rewrite ^/$ /frontend/ last;
}
我的問題是
location = / {} #Matches the path project.example.com only (mind there is a =)
location / {} #Matches every path (mind: there is no =)
您不需要為此的重寫規則。 只需使用
location / {
proxy_pass http://<your_backend>/frontend/;
}
我認為使用rewrite
不是完美的解決方案(順便說一句,我認為它不能涵蓋您問題的所有方面並可能導致新問題)
解決方法如下:
nginx config應該是這樣的:
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/;
}
}
或者如果您不使用sock
文件,則可以使用http方法。 例如,如果您在本地主機上使用端口8000運行django,則將其更改為:
proxy_pass http://localhost:8000/;
但是請記住,您應該在django settings.py
添加它。 除非它根本不起作用:
USE_X_FORWARDED_HOST = True
FORCE_SCRIPT_NAME = "/frontend"
通過這種方法,您正在django中更改基本URL。 因此,所有django網址都應以fronted
標簽開頭。 現在,nginx可以完美地充當您網站的反向代理了:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.