我的应用程序中有以下文件夹结构
/ app / server - 包含api的内容
/ app / static - 在相应的文件夹中包含图像,js和css文件,如(/ static / images,/ static / js ... etc)
upstream pythonapp {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name www.example.com;
error_log /home/LogFiles/docker/error.log warn;
root /app;
index index.html index.htm;
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 1d;
}
location /static {
autoindex on;
alias /app/static/;
}
location / {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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-NginX-Proxy true;
proxy_http_version 1.1;
proxy_pass http://pythonapp;
proxy_cache_bypass $http_upgrade;
}
}
一切都按预期工作但今天我意识到我们正在使用
http://xyz.abc/images/abc.gif
代替
http://xyz.abc/static/images/abc.gif
由于URL中没有静态,我/静态规则将不再受到尊重。
我已经在线阅读了不同方法的文章,但没有任何内容与URL相关,没有/ static。以前有人遇到过这个问题,并且知道解决方案吗?
新编辑:
我已将内容更改为@shdaws提到的内容。我不得不在服务器级别更改root以使其工作。
server {
listen 80;
server_name www.example.com;
error_log /home/LogFiles/docker/error.log warn;
root /app/static;
index index.html index.htm;
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 1d;
}
location ^~ /(audio|build|images|style) {
autoindex on;
root /app/static;
}
location / {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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-NginX-Proxy true;
proxy_http_version 1.1;
proxy_pass http://pythonapp;
proxy_cache_bypass $http_upgrade;
}
}
现在,它为/app/static/images
网址http://abc.xyz/images/test.jpg
文件夹中的静态内容提供服务。但http://abc.xyz/images/d1/test.jpg
不起作用。
编辑: - 以上配置有效,它也提供子文件夹内容。看起来我对.mp3文件有一些配置问题。
答案 0 :(得分:0)
将您的静态位置更改为图像,假设abc.gif
位于/app/static/abc.gif
:
# Serve /images/file.ext from /app/static/file.ext
location ^~ /images {
autoindex on;
alias /app/static;
}
如果您尝试使用/app/static/images/abc.gif
而不是别名来提供该文件,则会将该网址附加到根目录:
root
您还可以在所需目录中使用正则表达式:
# Serve /images/file.ext from /app/static/images/file.ext
location ^~ /images {
autoindex on;
root /app/static;
}
答案 1 :(得分:0)
感谢@sjdaws帮助解决此问题。下面的配置适用于我的场景。
worker_processes auto;
pid /var/run/nginx.pid;
daemon off;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /home/LogFiles/docker/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
upstream pythonapp {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name www.example.com;
error_log /home/LogFiles/docker/error.log warn;
root /app/static;
index index.html index.htm;
location ~* \.(js|css|png|jpg|jpeg|gif|ico|mp3|html)$ {
expires 1d;
}
location / {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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-NginX-Proxy true;
proxy_http_version 1.1;
proxy_pass http://pythonapp;
proxy_cache_bypass $http_upgrade;
}
}
}