Nginx重写规则未按预期工作

时间:2019-09-10 20:01:31

标签: php nginx url post

我有一个网站 https://dev.mywebsite.com/index.php?album=portraits 会根据POST值动态显示照片相册。

我想将此URL重写为此: https://dev.mywebsite.com/portraits

但是我的Nginx重写规则不起作用。输入https://dev.mywebsite.com/index.php?album=portraits时没有任何反应。输入https://dev.mywebsite.com/portraits时找不到页面。

我不知道我在做什么错。

这是我目前正在尝试使用的代码:

location / {
        rewrite ^/(.*)$ /album.php?album=$1 last;
}

我也尝试过:

location = /portraits { 
        rewrite ^/portraits?$ /index.php?album=portraits break; 
}

这:

location = /album { 
        rewrite ^album/([a-z]+)/?$ album.php?album=$1 break; 
}

这是我正在使用的整个nginx站点配置文件:

server {
        listen 80 default_server;
        listen 443 ssl;                                         
        root /config/www;
        index index.php index.htm index.html;
        server_name dev.mywebsite.com;

        ssl_certificate /config/keys/cert.crt;
        ssl_certificate_key /config/keys/cert.key;

        client_max_body_size 0;

        location / {
                try_files $uri $uri/ /index.html /index.php?$args =404;
        }
    location / {
                rewrite ^/(.*)$ /album.php?album=$1 last;
        }

    location ~ \.php$ {
                #fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # With php5-cgi alone:
                fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                #fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include /etc/nginx/fastcgi_params;

        }
}

1 个答案:

答案 0 :(得分:2)

如果您真正拥有的文件是index.php,则可以使用以下内容重写(不添加任何location):

rewrite ^/portraits$ /index.php?album=portraits last;

请当然保留location ~ \.php$ {:)

相关问题