重写规则不适用于 nginx

Rewrite rules not working on nginx

我在 sites-available/myapp.conf 中有以下重写规则。我的设置是 Nginx,php5-fpm.

    location / {
            try_files $uri $uri/ /index.php;

            # also tried
            # try_files $uri $uri/ /index.php$is_args$args;
            # try_files $uri $uri/ /index.php$args;

    }
            # also tried
            # replacing last with break

            rewrite "^/([a-zA-Z]+)/?$" "/index.php?lang=" last;
            rewrite "^/([a-zA-Z]+)/([a-zA-Z0-9]+)/?$" "/index.php?lang=&page=" last;

    location ~ \.php {

            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_nam$
            fastcgi_param SCRIPT_NAME $fastcgi_script_name;

            #fastcgi_pass unix:/var/run/php5-fpm.sock;

            fastcgi_index index.php;
            fastcgi_pass 127.0.0.1:9000;
    }

我也试过将重写规则包装在@rewrite 块中,但没有成功。

location / {
    try_files $uri @rewrite;
}
location @rewrite {
    #rewrite rules
}

手动传递变量确实有效

index.php?lang=en&page=pageName&type=typeName

有人可以帮忙解决这个问题吗?已经过了一整晚,但运气不好。

问题似乎是 fpm 进程仍然处于活动状态或以下问题导致了错误:

2016/06/03 02:06:24 [emerg] 6604#6604: bind() to 0.0.0.0:80 failed (98: Address already in use)
2016/06/03 02:06:24 [emerg] 6604#6604: still could not bind()

每次更改成功后我都重新启动了 nginx,但重写规则不起作用。使用

sudo fuser -k 80/tcp

问题解决了。希望这对其他人有用。

其中:

fuser - 识别使用文件或套接字的进程

-k 终止访问文件的进程。

(来源:http://geektechinfo.blogspot.nl/2013/09/starting-nginx-nginx-emerg-bind-to.html)

如果 fuser 不起作用,请尝试杀死所有活动的 nginx 进程:

sudo killall -KILL nginx

并(重新)启动 nginx:

sudo service nginx restart