重写不适用于nginx的规则

时间:2016-06-02 23:38:44

标签: php nginx php-5.5

我在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=$1" last;
            rewrite "^/([a-zA-Z]+)/([a-zA-Z0-9]+)/?$" "/index.php?lang=$1&page=$2" 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

有人可以帮忙解决这个问题。花了整个晚上但没有运气。

1 个答案:

答案 0 :(得分:1)

问题似乎是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终止访问该文件的进程。

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

如果热凝器不起作用,请尝试终止所有活动的nginx进程:

sudo killall -KILL nginx

并且(重新)启动nginx:

sudo service nginx restart