nginx通过uWSGI在一个子目录中提供Django

我已经完成了一些以前的主题:
How do I set subdirectory in nginx with Django
how to deploy django under a suburl behind nginx
Serving flask app on subdirectory nginx + uwsgi

基本的教训是,您只需要配置您的站点(可用)即可实现此目的.我现在尝试了各种各样的排列

server {
listen 80;
server_name www.example.com;

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
    root /path/to/project;
}

location /project/ {
    root            /path/to/project;
    include         /etc/nginx/uwsgi_params;
    uwsgi_param     SCRIPT_NAME /project;
    uwsgi_modifier1 30;
    uwsgi_param PATH_INFO "$1";
    uwsgi_pass      unix:/tmp/project.sock;
}}

当我将位置定义为“/”时,一切都运行得很好(并删除SCRIPT_NAME,modifier1,PATH_INFO和root并不重要.但是尝试使用子目录总是会导致找不到页面(404):

Request URL:    http://www.example.com/project/project

(编辑)它正在为请求添加目录.我怎么不搞清楚?

(尝试过forced_script_name – 不应该使用它并给出其他类型的头痛 – 和uwsgi配置设置)

编辑:

location /project/ {
    root            /path/to/project;
    include         /etc/nginx/uwsgi_params;
    uwsgi_param     SCRIPT_NAME /project;
    uwsgi_pass      unix:/tmp/project.sock;
}

不起作用…套接字在那里,当我配置/时工作 – 我只是看不到我错过的东西.

更新:

location ~ /project(?<path_info>/.*|$) {
    include         /etc/nginx/uwsgi_params;
    uwsgi_pass      unix:/tmp/project.sock;
    uwsgi_param     PATH_INFO $path_info;
    uwsgi_param     SCRIPT_NAME /project;
}

这会加载网站,但所有链接都指向http://example.com/link/to/something而不是http://example.com/project/link/to/something

在uWSGI中不推荐使用nginx uwsgi_modifier1.

您的目标是能够从任何地方托管wsgi应用程序,而无需对应用程序进行调整以考虑其所在的位置.

在uWSGI中执行此操作的当前方法是为每个URI应用程序组合映射挂载点,如下所示:

[uwsgi]
socket = 127.0.0.1:3031
; mount apps
mount = /app1=app1.py
mount = /app2=app2.py
; rewrite SCRIPT_NAME and PATH_INFO accordingly
manage-script-name = true

Hosting multiple apps in the same process (aka managing SCRIPT_NAME and PATH_INFO)

mount可以取代模块

特别是Django,

; Before
module = django_app.wsgi:application
; After
mount = /django_app=django_app.wsgi:application
manage-script-name = true
相关文章
相关标签/搜索