【问题标题】:Nginx Mod_RewriteNginx Mod_Rewrite
【发布时间】:2016-04-24 18:53:43
【问题描述】:

您好,我正在尝试正确设置 mod_rewrite,我对 lighttpd 没有任何问题,但我也想交换 nginx。

location /highscores {
    rewrite  ^/highscores  /?page=highscores last;
} 
location /highscores/ {
    rewrite  ^/highscores/(.*)/(.*)$  /?page=highscores&list=$1&page=$2 last;
}

问题是,如果你输入诸如 /highscores/ 之类的方向,最后 / 会出现错误 404,如果你尝试 /highscores/example 也会出现 404,但 /highscores/example/ 工作正常,有没有人能解决这个问题?

我在谷歌上找不到解决方案。

配置

server {
        listen   80;
    server_name example.com;
    root /usr/share/nginx/www/example.com;
    include /etc/nginx/include.d/all-common;
    include /etc/nginx/include.d/example.com;
}

通用

    index index.php index.html index.htm;
    try_files $uri $uri/ /index.html;
location / {
    try_files $uri $uri/ = /;
}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;

    }

include.d 中的 example.com 有我粘贴在顶部的其余代码

【问题讨论】:

  • 正则表达式 ^/highscores/(.*)/(.*)$ 既不匹配 /highscores/ 也不匹配 /highscores/example 因此此重写不适用。你期待什么重写?
  • 另外,我很好奇,你为什么要两个 page 参数?
  • 我只需要一个,但它根本不起作用。 example.com/highscores 显示 404 example.com/highscores/ 显示 404 example.com/highscores/example 正在工作,但不应该,它显示错误的 parm list=$1
  • @Edited 我添加了完整配置

标签: mod-rewrite nginx


【解决方案1】:

我也做过同样的事情,所以我很清楚你的感受。 :)

请获取此项目,并测试您的设置: https://github.com/TeXXaS/nginx-configs

重点是:

error_log /var/log/nginx/testing-rewrite-error.log debug;
rewrite_log on;

这样你就可以看到发生了什么。

Nginx 有类似内部重定向的功能。因此,如果您触摸indexrewrite - 这意味着将重新处理新的网址。如果你查看那个 git repo - 有产品解决方案,用于部分重写。这意味着 example.com/products/ 指向一个脚本,而 example.com/product/the_product - 转到另一个位置。

【讨论】:

    猜你喜欢
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 2014-08-13
    • 2013-02-02
    • 2012-07-28
    • 2015-09-20
    • 2012-07-19
    • 2021-11-26
    相关资源
    最近更新 更多