所有页面都显示404未找到(Nginx),但首页正常工作。

5

我正在运行一个PHP网站,其中所有子页面都显示404 Not Found Nginx错误,但主页和管理面板工作正常。我正在运行在Nginx服务器上的PHP网站。

以下是我的默认Nginx文件:

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

# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

root /var/www/html;

# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;

server_name (My IP Pasted here);

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
}

# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
    include snippets/fastcgi-php.conf;

    # With php-fpm (or other unix sockets):
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    # With php-cgi (or other tcp sockets):
    #fastcgi_pass 127.0.0.1:9000;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
    deny all;
}}   

以下是Nginx.conf文件:


user www-data;  
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 100000;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

这些是我的文件,主页可以正常使用,但其他子页面出现404 not found nginx错误。连管理员面板也不能正常工作。

请帮助我解决这个问题...


尝试将行try_files $uri $uri/ =404;更改为try_files $uri $uri/ /index.php$is_args$args;。顺便说一句,如果您的网站是新的,则将worker_connections 100000;设置为100000是有原因的,但1000足以开始使用。 - hcheung
无法工作,仍然面临相同的错误。 - Vijay Singh
你重启了nginx吗?你正在运行WordPress还是其他什么网站? - hcheung
是的,我已经重新启动了nginx,但仍然显示“404 Not Found nginx/1.15.9 (Ubuntu) error”。不,我没有使用WordPress,我正在使用Digitalocean托管,并简单地创建了droplet,然后安装了第一个nginx,然后安装了PHP-FPM 7.2,然后创建了数据库,然后安装了PhpMyAdmin。之后,我使用filezilla在我的目录-> /var/www/html上安装了一个PHP网站。即使我在本地的XAMP上尝试并测试了相同的脚本,它也可以正常运行和工作。 - Vijay Singh
请检查编辑。 - Vijay Singh
这是我的Nginx默认错误日志文件 --- 2019/05/02 01:57:29 [emerg] 14537#14537:在/etc/nginx/sites-enabled/default:92中意外结束文件,期望“}” 2019/05/02 01:59:13 [notice] 14565#14565:信号进程已启动 2019/05/02 02:02:06 [notice] 15984#15984:信号进程已启动 2019/05/02 02:21:13 [notice] 26291#26291:信号进程已启动 2019/05/02 02:24:54 [notice] 26307#26307:信号进程已启动 2019/05/02 02:32:12 [notice] 26333#26333:信号进程已启动 2019/05/03 02:11:48 [notice] 2385#2385:信号进程已启动 -- - Vijay Singh
2个回答

9

这里是nedwos解决该问题的方案:

将受影响网站位置块中的行更改为:

try_files $uri $uri/ /index.php?q=$uri&$args;

重新加载nginx

sudo systemctl reload nginx

一切应该没问题 :)

原问题链接


0

尝试在默认的nginx文件的位置路径中删除try_files $uri $uri/ =404;,然后检查是否有效。

如果无效

请在位置路径中删除try_files $uri $uri/ =404;,并添加以下内容到您的本地主机。

proxy_pass http://localhost:5000; #whatever port your app runs on
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;

粘贴到位置路径


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接