如果应用程序映射到子目录,如何在Nginx + Flask安装程序中加载静态文件

我有一个基本的网站运行在一个域名(例如example.com),我想在一个子目录(例如:example.com/test)中运行烧瓶应用程序烧瓶应用程序运行在端口5433使用默认烧瓶开发服务器。

我使用下面的nginxconfiguration来实现这一点

location /test { rewrite /test(.*) $1 break; proxy_pass http://localhost:5433; proxy_redirect default; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /static/{ alias /home/hrishi/www/example.com/test/app/static/; } 

我能够使用example.com/test/ url访问应用程序,但是静态文件无法加载(出现404错误),尽pipe有别名。

我怎样才能解决这个问题?

更新:按照指示添加整个服务器块

  server { listen *:80 default_server; ## listen for ipv4; this line is default and implied #listen [::]:80; # listen for ipv6 root /home/hrishi/www/example.com/; index index.php index.html index.htm; # Make site accessible from http://localhost/ server_name example.com; # Logging access_log /home/hrishi/log/example.com/access.log; error_log /home/hrishi/log/example.com/error.log notice; location / { #try_files $uri $uri/ /index.html; try_files $uri $uri/ /index.php; } location /doc { autoindex on; } location /test { rewrite /test(.*) $1 break; proxy_pass http://localhost:5433; proxy_redirect default; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /static/{ alias /home/hrishi/www/example.com/test/app/static/; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root /home/hrishi/www/example.com/error/; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # try_files $uri =404; # fastcgi_split_path_info ^(.+\.php)(/.+)$; # fastcgi_pass unix:/var/run/php5-fpm.sock; # fastcgi_index index.php; # include fastcgi_params; #} # serve static files directly location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ { expires max; } } 

这是因为:

 location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ { expires max; } 

因为正则expression式位置优先于前缀位置。 我会可耻地autopromote我的职位你应该阅读: Nginx rewite规则403错误 。

您将需要将以前的前缀位置块转换为正则expression式位置块,或者使用正则expression式位置块用于捕获组的文件扩展名,并在别名指令中使用后者。