从 Apache 迁移到 Nginx
+ php-fpm
后 tweetnest 的规则一直没搞定, tweetnest
本身可用,但按月分页 tweetnest/yyyy/mm
会 404
。
tweetnest 默认只提供 apache 重写规则 .htaccess)
如下。但未提供 Nginx 重写规则配置。
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
找到 https://github.com/graulund/tweetnest/issues/37 不过该配置是给 CNAME 的,而我是直接是 domain.com/tweetnest
目前 Nginx vhost 配置块如下,访问 domain.com/tweetnest/yyyy/mm 会 404 请教一下如何修改。
# 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;
}
完整的 Nginx 配置如下,谢谢。
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/terry.im;
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;
}
}