Redirect 为什么Nginx将所有内容重写为index.php,不起作用?

Redirect 为什么Nginx将所有内容重写为index.php,不起作用?,redirect,url-rewriting,nginx,vhosts,Redirect,Url Rewriting,Nginx,Vhosts,我刚刚开始尝试使用Nginx,但在配置方面遇到了问题。我希望实现以下目标: 我希望每个流量都被重定向到index.php——这样我就可以使用路由创建干净的URL 是的,我想让它重写URL。在我将Apache用于此配置之前: RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.+)$ index.php?q=$1 [QSA,L] 这个

我刚刚开始尝试使用Nginx,但在配置方面遇到了问题。我希望实现以下目标:

  • 我希望每个流量都被重定向到
    index.php
    ——这样我就可以使用路由创建干净的URL
是的,我想让它重写URL。在我将Apache用于此配置之前:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.+)$ index.php?q=$1 [QSA,L]
这个Apache规则只是将每个流量重定向到
index.php
。我希望通过Nginx实现同样的目标

到目前为止,我已经有了这个代码,但它并没有真正起作用。它给了我502个错误和403个错误

server {
          error_log /var/log/nginx/vhost-error_log warn;
          listen *.*.*.*:80;
          listen [::]:80;
      server_name ***.com www.***.com;
          access_log /usr/local/apache/domlogs/***.com-bytes_log bytes_log;
          access_log /usr/local/apache/domlogs/***.com combined;
          root /home/***/public_html;
          #location / {
          location ~*.*\.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|html|htm|txt|js|css|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso)$ {
          expires 1M;
          try_files $uri @backend;
          }

          location @backend {
          internal;
          proxy_pass http://*.*.*.*:8081;
          include proxy.inc;
      include microcache.inc;
          }

          location ~ /\.ht {
          deny all;
          }
          location / {
              set $page_to_view "/index.php";
              try_files $uri $uri/ @rewrites;
              root   /home/***/www/public_html/ ;
              index  index.php index.html index.htm;
          }

          location ~ \.php$ {
              include /etc/nginx/fastcgi_params;
              fastcgi_pass  127.0.0.1:9000;
              fastcgi_index index.php;
              fastcgi_param SCRIPT_FILENAME /home/***/www/public_html/$page_to_view;
          }

          # rewrites
          location @rewrites {
              if ($uri ~* ^/([a-z]+)$) {
                  set $page_to_view "/$1.php";
                  rewrite ^/([a-z]+)$ /$1.php last;
              }
          }
        }
我知道这是原始配置和我在谷歌上找到的东西的混合。有谁能帮我制作一个有用的文件来完成我需要的工作吗?

尝试更改:

try_files $uri $uri/ @rewrites;
致:

这将相当于您的apache重写

此外,您的复制/粘贴配置可能没有意义。最好从空的开始。你需要

location / {
  try_files $uri $uri/ /index.php?q=$uri;
  root ***;
}
\.php
开始

location / {
  try_files $uri $uri/ /index.php?q=$uri;
  root ***;
}