Nginx URL 重写文件未找到

Nginx URL 重写文件未找到

我已经将其转换.htaccess为使用 Nginx,但是我遇到了许多 404 错误或文件夹和子文件夹重写错误显示。

此 Nginx 配置适用于文件夹和子文件夹,但/view/它将/home/website/www/index.php

.htaccess

RewriteRule ^view/execute(.+)$ /home/website/view/exec.php?r=$1 [QSA,L]
RewriteRule ^view/status$ /home/website/view/status.php [QSA,L]

RewriteRule ^(.+)/$ /home/website/www/index.php?a=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /home/website/www/index.php?a=$1 [QSA,L]

Nginx 配置如下:

listen   80; ## listen for ipv4; this line is default and implied
listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

root /home/website/www;
index index.php index.html index.htm;

# Make site accessible from http://localhost/
server_name example.com;

error_page 404 /home/website/error/index.php?er=404;

location / {
    set $1 "/index.php";
    try_files $uri $uri/ @rewrites;
    root   /home/website/www;
    index  index.php index.html index.htm;
}

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

# rewrites
location @rewrites {
   rewrite ^/(.*)$ /home/website/www/index.php?a=$1;
   rewrite ^view/execute(.*)$ /home/website/view/execute.php?a=$1;
   #rewrite ^view/(.*)$ /home/website/view/index.php?a=$1 last;
}

谢谢。

编辑

我有这个错误: [error] 2329#0: *1 FastCGI sent in stderr: "Unable to open primary script: /home/website/www/home/website/www/index.php (No such file or directory)" while reading response header from upstream, request: "GET /view/execute/ HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:"

使用别名

location / {
    try_files $uri $uri/ /index.php?a=$uri;
}
location /view/execute {
    index test.txt;
    alias /home/website/view;
}

它的工作test.txt,但如果我改变,test.php我就会遇到这个错误'No input file specified.'

我的文件夹是这样的:

- /home/
  - /website/
    - /view/
      - execute.php
      - test.php
      - test.txt
    - /www/
      - index.php 
    - /function/

像 example.com/registre/confirm 这样的 url 可以工作,但是这个 url example.com/view/execute 不起作用 :/ 我测试了使用递归为文件夹 /view/ 授予权限 +x,但是不起作用

抱歉问了这么多问题,我是菜鸟,哈哈

答案1

你的 nginx 配置一点都不好。尝试这样做:

listen 80;
listen [::]:80 default_server ipv6only=on;

root /home/website/www;
index index.php index.html index.htm;

server_name example.com;

error_page 404 /home/website/error/index.php?er=404;

location / {
    rewrite ^view/execute(.*)$ /home/website/view/execute.php?a=$1;
    try_files $uri $uri/ /home/website/www/index.php?a=$uri;
}

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

因此,您的配置中的主要问题是:

  1. @rewrites 块中的重写行顺序错误,因此后面的重写从未达到,因为第一个重写规则始终匹配。
  2. PHP 位置块有一个不需要的 try_files 规则,这可能会导致 PHP 文件出现问题。

如果上述配置不起作用,您可以尝试try_files用以下行替换该行:

rewrite ^/(.*)$ /home/website/www/index.php?a=$1;
try_files $uri $uri/ =404;

相关内容