nginx重写tweetnest的规则

最近我把我的最后一个Apache迁移到了nginx(+ php-fpm)。 除了tweetnest月份链接(tweetnest / yyyy / mm)以外,一切正常。

tweetnest提供的Apache重写规则( .htaccess )工作正常OOTB

Apache的.htaccess

 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没有例子。

我设法find这个https://github.com/graulund/tweetnest/issues/37

但这是CNAME,与我的tweetnest用例 – > domain.com/tweetnest不完全一样。

我尝试了各种组合,但没有得到它的工作。

目前我在nginx vhostconfiguration文件中使用下面的块

  # 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错误。

完整的虚拟主机configuration如下所示

 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; } 

您在该位置内设置了root指令。 这不是一个好主意。 如果你需要为tweetnest文件指定一个单独的位置,你应该使用alias指令。

所以,如果你的tweetnest sort.php位于/var/www/tweetnest/sort.php ,你的configuration应该是这样的:

编辑:修改后的新信息在下面的评论。

 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; }