Nginx 用 Nginx + php-fpm 跑 tweetnest 重写规则求教

terrywang · 2014年06月25日 · 最后由 psvr 回复于 2014年06月30日 · 7902 次阅读

从 Apache 迁移到 Nginx + php-fpm 后 tweetnest 的规则一直没搞定, tweetnest 本身可用,但按月分页 tweetnest/yyyy/mm404

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;
    }
}
  1. location ~ /tweetnest {下面插入 return 403,执行 nginx -s reload 后,访问/tweetnest 看是不是 403 确认 location 的逻辑到这里了;
  2. 好像 rewrite 里面要把匹配的前缀写上,比如 rewrite ^/(tweetnest/.*) xxxx或者是 rewrite ^(.*) xxxx;

#1 楼 @debugger 逻辑走到了。

多谢你的提示,改成下面这样就可以了,终于解决了 ;-D

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

楼主的头像……

#3 楼 @psvr 乐高而已,有什么,嘿嘿。

哈哈哈 乐高的敏感造型

需要 登录 后方可回复, 如果你还没有账号请 注册新账号