我正在从 mod_php 迁移到 nginx。除了这个重写之外,一切都正常。但我对 nginx 配置不够熟悉,不知道正确的做法。
我通过查看 nginx 网站上的示例想到了这一点。
server {
server_name test01.www.myhost.com;
root /home/vhosts/my_home/blah;
access_log /var/log/nginx/blah.access.log;
error_log /var/log/nginx/blah.error.log;
index index.php;
location / {
try_files $uri $uri/ @rewrites;
}
location @rewrites {
rewrite ^ /index.php last;
rewrite ^/ht/userGreeting.php /js/iFrame/index.php last;
rewrite ^/ht/(.*)$ /$1 last;
rewrite ^/userGreeting.php$ /js/iFrame/index.php last;
rewrite ^/a$ /adminLogin.php last;
rewrite ^/boom\/(.*)$ /boom/index.php?q=$1 last;
rewrite ^favicon.ico$ favico_ry.ico last;
}
# This block will catch static file requests, such as images, css, js
# The ?: prefix is a 'non-capturing' mark, meaning we do not require
# the pattern to be captured into $1 which should help improve performance
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
# Some basic cache-control for static files to be sent to the browser
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
include php.conf;
}
我遇到的问题与此重写有关:
rewrite ^ht\/(.*)$ /$1 last;
99% 的请求都会影响这次重写,这些请求都是静态文件。所以我认为它可能被发送到了静态文件部分,而这正是事情被搞乱的地方?
我尝试添加这个,但没有效果:
location ~* ^ht\/.*\.(?:ico|css|js|gif|jpe?g|png)$ {
# Some basic cache-control for static files to be sent to the browser
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
任何帮助都将不胜感激。我知道最好的办法就是在代码中将 /ht/whatever.jpg 的引用更改为 /whatever.jpg...但目前还不行。
答案1
尝试这个:location ~ ((ht/.*)|(?:ico|css|js|gif|jpe?g|png)$)
答案2
我通过添加解决了这个问题
try_files $uri $uri/ @rewrites;
到静态文件位置块。