【问题标题】:Nginx showing 404 Not Found for every page but homepageNginx 显示 404 Not Found 为除主页之外的每个页面
【发布时间】:2016-10-14 07:46:13
【问题描述】:

我正在尝试在 ubuntu 的 nginx 服务器上运行一个 php 应用程序。我确实将所有文件都放在了 var/www/mydomain.com.html 中,它完美地呈现了我的 index.php。但是即使这些文件存在于同一目录中,也会为每个其他页面显示 404 Not Found。

这是我的服务器块配置。

server {
listen   80; ## listen for ipv4; this line is default and implied
listen   [::]:80; ## listen for ipv6

root /var/www/app.limoposter.com/html;
index index.php index.html index.htm;

server_name app.limoposter.com www.app.limoposter.com;

location / {
    try_files $uri $uri/ =404;

}

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)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

}

【问题讨论】:

    标签: php web-services .htaccess nginx vps


    【解决方案1】:

    您似乎有一个 .htaccess 将每个请求路由到您的 index.php 由于 nginx 不理解 .htaccess 文件,因此您必须使用 nginx 重写它 见https://winginx.com/en/htaccess

    编辑: 我下载了你的 .htaccess 并使用了上面提到的转换器: 这是输出:

    autoindex off;
    location / {
    if ($script_filename !~ "-d"){
    rewrite ^/login/(.*)/$ /login.php?type=$1 break;
    }
    if ($script_filename !~ "-d"){
    rewrite ^/dashboard/(.*)/$ /dashboard.php?show=$1 break;
    }
    if ($script_filename !~ "-d"){
    rewrite ^/admin/(.*)/$ /admin.php?module=$1 break;
    }
    if ($script_filename !~ "-d"){
    rewrite ^/(.*)/$ /$1.php break;
    }
    if ($request_uri ~ "storage/(\d+)_(.*)$"){
    rewrite ^/storage/(\d+)_(\d+)/([a-zA-Z0-9_]+).([a-zA-Z0-9_\.]+)$ /download.php?folder=$1_$2&file=$3.$4 break;
    }
    }
    

    【讨论】:

    • 我应该把这个重写的htaccess放在服务器块、nginx.conf还是其他任何地方?
    • 这应该可以,但为了更有条理,您可以创建一个额外的文件
    • 感谢您的建议 :)
    猜你喜欢
    • 2015-10-16
    • 2019-09-20
    • 2018-12-03
    • 1970-01-01
    • 2021-07-25
    • 2021-03-07
    • 1970-01-01
    • 2015-05-31
    • 2021-06-05
    相关资源
    最近更新 更多