![nginx 重写 tweetnest 规则](https://linux22.com/image/629647/nginx%20%E9%87%8D%E5%86%99%20tweetnest%20%E8%A7%84%E5%88%99.png)
最近我将最后一个 apache 迁移到了 nginx (+ php-fpm)。除了按月链接的 tweetnest (tweetnest/yyyy/mm) 之外,其他一切都运行正常。
tweetnest 提供了 apache 重写规则 ( .htaccess
),可以在 OOTB 中正常工作
.htaccess
对于 Apache
RewriteEngine On
RewriteBase /tweetnest
RewriteRule ^sort/?$ ./sort.php [L]
RewriteRule ^favorites/?$ ./favorites.php [L]
RewriteRule ^search/?$ ./search.php [L]
RewriteRule ^([0-9]+)/([0-9]+)/?$ ./month.php?y=$1&m=$2
RewriteRule ^([0-9]+)/([0-9]+)/([0-9]+)/?$ ./day.php?y=$1&m=$2&d=$3
但是,没有 nginx 的示例。
我找到了这个https://github.com/graulund/tweetnest/issues/37
但它适用于 CNAME,与我的 tweetnest 用例不完全相同 -> domain.com/tweetnest
。
我尝试了各种组合但都未能使其发挥作用。
目前我在 nginx vhost 配置文件中使用以下块
# tweetnest rewrite rules
location ~ /tweetnest {
root /var/www/path;
rewrite ^/sort/?$ sort.php last;
rewrite ^/favorites/?$ favorites.php last;
rewrite ^/search/?$ search.php last;
rewrite ^/([0-9]+)/([0-9]+)/?$ month.php?y=$1&m=$2;
rewrite ^/([0-9]+)/([0-9]+)/([0-9]+)/?$ day.php?y=$1&m=$2&d=$3;
}
点击 tweetnest/yyyy/mm 链接时出现 404 错误。
完整的 vhost 配置如下
upstream php {
server unix:/var/run/php5-fpm.sock;
}
server {
listen *:80;
root /var/www/path/to/root;
index index.php index.html index.htm;
server_name domain.com www.domain.com;
# rewrite ^(.*)$ $scheme://www.domain.com$1;
access_log /var/log/nginx/domain.com-access.log;
error_log /var/log/nginx/domain.com-error.log;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_index index.php;
include fastcgi_params;
# fastcgi_pass unix:/var/run/php5-fpm.sock;
# Use upstream
fastcgi_pass php;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ /\. {
deny all;
}
location ~* (?:\.(?:bak|config|sql|fla|psd|ini|log|sh|inc|swp|dist)|~)$ {
deny all;
}
# Browser cache
location ~* ^.+\.(css|js|jpg|jpeg|gif|png|ico|gz|svg|svgz|ttf|otf|woff|eot|mp4|ogg|ogv|webm)$ {
expires 30d;
log_not_found off;
}
# tweetnest rewrite rules
location ~ /tweetnest {
# root /var/www/domain.com;
rewrite ^/sort/?$ sort.php last;
rewrite ^/favorites/?$ favorites.php last;
rewrite ^/search/?$ search.php last;
rewrite ^/([0-9]+)/([0-9]+)/?$ month.php?y=$1&m=$2;
rewrite ^/([0-9]+)/([0-9]+)/([0-9]+)/?$ day.php?y=$1&m=$2&d=$3;
}
}
任何帮助都将不胜感激;-)
2014年6月27日更新。
通过更改重写规则修复了以下问题
# tweetnest rewrite rules
location ~ /tweetnest {
rewrite ^/tweetnest/sort/?$ /tweetnest/sort.php last;
rewrite ^/tweetnest/favorites/?$ /tweetnest/favorites.php last;
rewrite ^/tweetnest/search/?$ /tweetnest/search.php last;
rewrite ^/tweetnest/([0-9]+)/([0-9]+)/?$ /tweetnest/month.php?y=$1&m=$2;
rewrite ^/tweetnest/([0-9]+)/([0-9]+)/([0-9]+)/?$ /tweetnest/day.php?y=$1&m=$2&d=$3;
}
答案1
您root
在位置内设置指令。这不是一个好主意。alias
如果您需要为 tweetnest 文件指定单独的位置,则应使用该指令。
因此,如果你的 tweetnestsort.php
位于/var/www/tweetnest/sort.php
,你的配置应该如下所示:
编辑:根据下面评论中的新信息进行修改。
root /var/www/terry;
# tweetnest rewrite rules
location /tweetnest {
rewrite ^/tweetnest/sort/?$ /tweetnest/sort.php last;
rewrite ^/tweetnest/favorites/?$ /tweetnest/favorites.php last;
rewrite ^/tweetnest/search/?$ /tweetnest/search.php last;
rewrite ^/tweetnest/([0-9]+)/([0-9]+)/?$ /tweetnest/month.php?y=$1&m=$2 last;
rewrite ^/tweetnest/([0-9]+)/([0-9]+)/([0-9]+)/?$ /tweetnest/day.php?y=$1&m=$2&d=$3 last;
}