Python 如何使用uwsgi在同一个nginx服务器的不同位置上为不同的flask应用程序执行url重写?
我面临以下问题: 我已经设置了nginx来监听两个不同的上游应用服务器,它们在不同的端口上使用uwsgi运行。这两个应用程序使用相同的静态文件,但第一个应用程序或第二个应用程序的加载取决于nginx的位置:Python 如何使用uwsgi在同一个nginx服务器的不同位置上为不同的flask应用程序执行url重写?,python,regex,nginx,url-rewriting,uwsgi,Python,Regex,Nginx,Url Rewriting,Uwsgi,我面临以下问题: 我已经设置了nginx来监听两个不同的上游应用服务器,它们在不同的端口上使用uwsgi运行。这两个应用程序使用相同的静态文件,但第一个应用程序或第二个应用程序的加载取决于nginx的位置: server { listen 80; server_name localhost; charset utf-8; client_max_body_size 75M; # Proxy connections to the applicati
server {
listen 80;
server_name localhost;
charset utf-8;
client_max_body_size 75M;
# Proxy connections to the application servers
# app_servers
location /firstapplication {
include /etc/nginx/mime.types;
proxy_pass http://app_servers_firstapplication/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
rewrite /?(?!/firstapplication)/(.+) /firstapplication/$1 last;
}
location /secondapplication {
include /etc/nginx/mime.types;
proxy_pass http://secondapplication/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
rewrite /?(?!/secondapplication)/(.+) /secondapplication/$1 last;
}
location ^~ /scripts/ {
alias /webapps/scripts/;
}
location ^~ /css/ {
alias /webapps/html/css/;
}
location ^~ /graphics/ {
alias /webapps/html/graphics/;
}
location ^~ /fonts/ {
alias /webapps/html/fonts/;
}
}
当我调用mydomain/firstapplication或mydomain/secondapplication时,相应应用程序的初始页面会正确加载
但是当应用程序的初始页面被加载,我点击应用程序中的链接时,它使用的是应用程序的url,比如/contact而不是/firstapplication/contact,因此我在应用程序中调用的每个url都会出现404错误
我想用nginx中特定于位置的重写来解决这个问题,如下所示:
rewrite /?(?!/firstapplication)/(.+) /firstapplication/$1 last;
及
我使用了一个否定的lookback断言来避免加载页面/fistapplication时的循环重定向。
这个重写正则表达式应该可以工作我在这个在线编辑器中测试过它:
不幸的是,nginx没有重定向应用程序url,它们仍然会导致404错误
我可以使用nginx重写模块来实现我的目标吗?或者,我可以使用哪些其他方法在同一台nginx服务器上运行两个不同的应用程序?
rewrite /?(?!/secondapplication)/(.+) /secondapplication/$1 last;