nginx 重写子 url

nginx rewrite sub url

我该如何解决这个问题:我想设置 nginx conf 文件以满足以下条件:

http://www.example.com/site1/article/index.php?q=hello-world -> http://www.example.com/site1/article/hello-world

httb://www.example.com/site2/article/index.php?q=goodbye-world -> httb://www.example.com/site2/article/goodbye-world

httb://www.example.com/site3/article/index.php?q=open-new-world -> httb://www.example.com/site3/article/open-new-world

example.com之后有多个站点,我想使用nginx配置让url看起来干净

但是我的以下配置不起作用。有人帮帮我吗?

server {
listen 80;
listen [::]:80;

root /var/www/example.com/public_html;
index index.php index.html index.htm;

server_name www.example.com;     
location ~ /article/ {
    try_files $uri /site1/article/index.php?q=;

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }
}

}

您希望客户端呈现一个 URL,例如 /xxx/article/yyy,然后在内部重写为 /xxx/article/index.php?q=yyy

您需要捕获源 URI 的组件,以便稍后使用它们。您的问题中有一个 </code> ,但是您缺少实际给它赋值的表达式。只需最少的更改,就可以了:</p> <pre><code>location ~ ^(.*/article/)(.*)$ { try_files $uri index.php?q=; location ~ \.php$ { ... } }

但是,您不需要为 PHP 使用嵌套位置,只要 PHP 正则表达式位置出现在 上方 另一个正则表达式位置,它将处理所有 php 个文件。例如:

location ~ \.php$ { ... }

location ~ ^(.*/article/)(.*)$ {
    try_files $uri index.php?q=;
}