【发布时间】:2017-04-15 11:20:14
【问题描述】:
我刚刚从 Apache 切换到 nginx,但仍然需要一些时间来适应(并且需要大量学习)。
我正在运行一个具有以下配置的 Pagekit 网站:https://gist.github.com/DarrylDias/be8955970f4b37fdd682
server {
listen 80;
listen [::]:80;
# SSL configuration
listen 443 ssl;
listen [::]:443 ssl;
ssl on;
ssl_certificate /etc/ssl/private/mydomain.com.crt;
ssl_certificate_key /etc/ssl/private/mydomain.com.private.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_client_certificate /etc/ssl/private/cloudflare.origin-pull-ca.pem;
ssl_verify_client on;
server_name mydomain.com www.mydomain.com;
root /home/vhosts/domains/mydomain.com/public/;
index index.php;
# Leverage browser caching of media files for 30 days
location ~* \.(?:ico|css|js|gif|jpe?g|png|ttf|woff)\$ {
access_log off;
expires 30d;
add_header Pragma public;
add_header Cache-Control "public, mustrevalidate, proxy-revalidate";
}
location / {
try_files $uri $uri/ /index.php?$args;
}
# Deny access to sensitive folders
location ~* /(app|packages|storage|tmp)/.*$ {
return 403;
}
# Deny access to files with the following extensions
location ~* \.(db|json|lock|dist|md)$ {
return 403;
}
# Deny access to following files
location ~ /(config.php|pagekit|composer.lock|composer.json|LICENSE|\.htaccess) {
return 403;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php7-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_param HTTP_MOD_REWRITE On;
}
}
不幸的是,许多人(包括我)都遇到这样的问题,即扩展名为 js|css|jpg|<etc> 的文件会收到 403 响应,因为它们位于 app or packages 目录中。
我尝试了多个正则表达式来尝试在 nginx 中为这些文件赋予 location 更高的优先级,但它们似乎没有效果。
应如何更改此配置文件以允许此类文件,但仍对这些目录中的所有其他文件返回 403?
编辑:文件 URL 看起来像 https://example.com/app/js/something.min.js?v=1921 可能因为 ?v=1921 而不起作用?
【问题讨论】:
-
查询字符串不会被
location块匹配。您的 nginx 服务器是否落后于某些类型的 CDN?它可能是由那些 CDN 服务器上的缓存引起的。尝试将 URL 更改为 example.com/app/js/something.min.js?v=1922 以绕过缓存。 -
我正在使用 cloudflare,但我已将自己置于开发模式并确保清除缓存。我知道一个事实,因为 pagekit 本身的 url 是这样的
-
缓存由 Cloudflare 存储在服务器端,因此只有浏览器开发模式实际上不起作用。也许您可以将版本号从“1921”更改为任何其他字符串,然后在将最后一个
location移到顶部之后,看看它是否会起作用。 -
恐怕这不是我能改变的。我正在尝试查找/编写一个忽略查询字符串的正则表达式,但到目前为止还没有运气
-
其实可以的。直接复制url改参数,直接浏览js文件看看是否还是403返回。
标签: nginx