且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用于SEO友好URL结构的Nginx重写规则-WordPress和另一个Web应用程序

更新时间:2023-01-08 10:17:20

WordPress和 my-app 都在一个通用文档根目录下工作,这使事情变得简单.

Both WordPress and my-app work within a common document root which makes things simple.

try_files 指令的最后一个元素是(例如)SEO友好URL的默认操作.

The last element of the try_files directive is the default action for (for example) SEO friendly URLs.

您需要使用以/my-app 开头的URI的其他默认处理程序,例如,可以使用 location/my-app 块来实现:>

You need a different default handler for URIs which begin with /my-app, which is achieved using a location /my-app block, for example:

location / {
    try_files $uri $uri/ /index.php?$args;
}
location /my-app {
    try_files $uri $uri/ /my-app/file.php?arg=$uri&$args;
}
location ~ \.php$ {
    try_files $uri =404;
    ...
}

在上述情况下,将 arg 设置为值/my-app/value .如果确实必须提取URI的最后一部分,请添加带有重写的命名位置,例如:

In the above case, arg is set to the value /my-app/value. If you really must extract the last part of the URI, add a named location with a rewrite, for example:

location / {
    try_files $uri $uri/ /index.php?$args;
}
location /my-app {
    try_files $uri $uri/ @rewrite;
}
location @rewrite {
    rewrite ^/my-app/(.*)$ /my-app/file.php?arg=$1;
}
location ~ \.php$ {
    try_files $uri =404;
    ...
}

请注意,您需要将 fastcgi_read_timeout 300; (在您的问题中)放置在 location〜\ .php $ 块或外部块之一中,为了有效.

Note that the fastcgi_read_timeout 300; (in your question) needs to be placed in the location ~ \.php$ block, or in one of the outer blocks, in order to be effective.

有关使用 nginx 指令的详细信息,请参见以上.

See this for details of the nginx directives used above.