如何为特定URL启用Nginx catch?我有一个django网络应用程序,我把我的静态文件,如CSS,JavaScript和…放在一个名为静态的文件夹中,我可以通过http://example.com/static/访问它们,我想配置Nginx n命令只从这个网址捕获这些文件.我已经阅读了http://www.nginxtips.com/how-to-enable-browser-cache-static-files-on-nginx/但这个选项没有在那里解释.我尝试下面的配置并重新启动我的ngnix服务器,但我有错误.
这是我的站点可用/默认配置文件.有什么问题?
server {
listen 80;
listen 443 ssl;
server_name www.example.com example.com;
ssl_certificate /var/www/example/ssl/ssl.crt;
ssl_certificate_key /var/www/example/ssl/ssl.key;
location /static (jpg|jpeg|png|gif|ico|css|js)${
alias /var/www/example/static;
expires 3d;
add_header Pragma public;
add_header Cache-Control "public,must-revalidate,proxy-revalidate";
}
location /media {
alias /var/www/example/media;
}
location / {
proxy_pass http://127.0.0.1:8000;
}
}
2014/03/07 13:02:28 [error] 25189#0: *22 open() "/etc/Nginx/html/static/css/wait.css" Failed (2: No such file or directory),client: 2.178.27.157,server: www.example.com,request: "GET /static/css/wait.css HTTP/1.1",host: "example.com",referrer: "http://example.com/"
2014/03/07 13:02:28 [error] 25189#0: *23 open() "/etc/Nginx/html/static/img/infographi.png" Failed (2: No such file or directory),request: "GET /static/img/infographi.png HTTP/1.1",referrer: "http://example.com/"
2014/03/07 13:02:29 [error] 25189#0: *23 open() "/etc/Nginx/html/favicon.ico" Failed (2: No such file or directory),request: "GET /favicon.ico HTTP/1.1",host: "example.com"
2014/03/07 13:08:41 [emerg] 25220#0: invalid location modifier "/static" in /etc/Nginx/sites-enabled/default:8
2014/03/07 13:08:41 [emerg] 25221#0: invalid location modifier "/static" in /etc/Nginx/sites-enabled/default:8
2014/03/07 13:09:50 [emerg] 25255#0: invalid location modifier "/static" in /etc/Nginx/sites-enabled/default:8
2014/03/07 13:09:50 [emerg] 25256#0: invalid location modifier "/static" in /etc/Nginx/sites-enabled/default:8
最佳答案
从nginx wiki开始,你似乎在“/ static”和你的正则表达式模式之间有一个空格.位置只需要一个“uri”参数,而不是两个……并且Nginx会将该空格解释为参数分隔符.
此外,您需要告诉您想要使用’〜’或’〜*’修饰符匹配正则表达式的位置块.
你可以试试这个
location ~* "/static.*(jpg|jpeg|png|gif|ico|css|js)$" {
而不是你拥有的,看看它是否符合你的要求.