简体   繁体   English

nginx重写子网址

[英]nginx rewrite sub url

How can I solve this problem: I want to set up nginx conf file to meet below criteria: 我该如何解决此问题:我想设置nginx conf文件以满足以下条件:

http://www.example.com/site1/article/index.php?q=hello-world -> http://www.example.com/site1/article/hello-world 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/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 httb://www.example.com/site3/article/index.php?q = open-new-world-> httb://www.example.com/site3/article/open-new-world

There are multiple sites after example.com, I want to make the url look clean by using nginx configuration. example.com之后有多个站点,我想使用nginx配置使URL看起来干净。

But my below configuration doesn't work. 但是我下面的配置不起作用。 Someone help me? 谁来帮帮我?

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=$1;

    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;
    }
}

} }

You would like the client to present a URL like /xxx/article/yyy which is then internally rewritten to /xxx/article/index.php?q=yyy . 您希望客户端提供一个类似于/xxx/article/yyy的URL,然后将其内部重写为/xxx/article/index.php?q=yyy

You need to capture the components of the source URI in order to use them later. 您需要捕获源URI的组件,以便以后使用。 You have a $1 in your question, but you are missing the expression to actually give it a value. 您的问题中有$1 ,但实际上缺少为它赋值的表达式。 With the minimum number of changes, this works: 使用最少的更改,这可以工作:

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

However, you do not need to use a nested location for PHP, as long as the PHP regex location appears above the other regex location, it will process all php files. 但是,您不需要为PHP使用嵌套位置,只要PHP regex位置出现在另一个regex位置上方 ,它将处理所有php文件。 For example: 例如:

location ~ \.php$ { ... }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM