Nginx wordpress url 改写,另加一条规则

Nginx wordpress url rewriting, with another rule

我是 nginx 新手,正在配置我的服务器块。目前一切正常,但现在我正在尝试移动我在 Apache 服务器上的 wordpress 网站。 在我的网站上,我有一个重写规则,允许我在不指定 .php 扩展名的情况下访问 php 页面。我也有 wordpress 永久链接重写规则。

现在在 nginx 上,我设法让它们工作,但只能单独工作:

location / { # Accessing php script without specifying the extension try_files $uri $uri/ $uri.html $uri.php?$query_string; }

...和...

location / { # Wordpress permalinks try_files $uri $uri/ /index.php?q=$uri&$args; }

我想做的是让这两个规则一起工作。我不知道该怎么做,也没有找到符合我想要的东西。

非常感谢!

如果我正确理解你的用例,你应该能够像这样将两者组合成一个位置块:

location / {
  try_files $uri $uri/ $uri.html $uri.php /index.php?q=$uri&$args;
}

在那之后您仍然需要一个单独的位置块来处理 PHP。类似于:

location ~ \.php$ {
  #match actual filename with extension or file not found
  try_files $uri =404;

  #... or replace next lines with your preferred PHP processing config
  include fastcgi_params;
  fastcgi_pass unix:/var/run/php5-fpm.sock;
}

当然,这不是完整的配置,但希望在这方面有所帮助。

我确信你写给我的解决方案运行良好,但我不能很好地配置我的服务器。问题是未设置扩展名时会下载 php 页面。这是我的配置,感谢您的好意观看:

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

    root /var/www/site.net/html;

    index index.html index.htm index.php;

    server_name site.net www.site.net;

    location / {
        try_files $uri $uri/ $uri.html $uri.php /index.php?q=$uri&$args;
    }

    # PHP CONFIGURATION

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

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

(很抱歉回复 post,代码太长无法放入评论...)