配置nginx 解决404 not found问题

本文详细介绍了如何通过配置Nginx解决404 Not Found问题,包括针对80和443端口的server{}

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置nginx 解决404 not found问题


无论是配置80 还是443 server{}中

都要增加如下代码

location ~ \.php(.*)$  {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }

整个ngnix.conf代码如下

#  power by www.php.cn
#user  nobody;

worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    #tcp_nodelay on;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 128k;
  fastcgi_buffers 4 128k;
  fastcgi_busy_buffers_size 256k;
  fastcgi_temp_file_write_size 256k;

  #gzip  on;
  gzip on;
  gzip_min_length  1k;
  gzip_buffers     4 32k;
  gzip_http_version 1.1;
  gzip_comp_level 2;
  gzip_types       text/plain application/x-javascript text/css application/xml;
  gzip_vary on;
  gzip_disable "MSIE [1-6].";

  server_names_hash_bucket_size 128;
  client_max_body_size     100m; 
  client_header_buffer_size 256k;
  large_client_header_buffers 4 256k;

    server {
        listen       80;
        server_name  www.******.cn;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        root    "C:/phpStudy/PHPTutorial/WWW/blog/public/";
        location / {

            index  index.html index.htm index.php l.php;
 try_files $uri $uri/ /index.php?$query_string;
           autoindex  on;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php(.*)$  {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }

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


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    server {
        listen 443  ssl; 
        server_name  www.******.cn;
        ssl on;
         root    "C:/phpStudy/PHPTutorial/WWW/blog/public/";
        ssl_certificate      1_www.******.cn_bundle.crt;
        ssl_certificate_key  2_www.******.cn.key;
        ssl_session_timeout  5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;

     
        location / {
 
            root    "C:/phpStudy/PHPTutorial/WWW/blog/public/";
            try_files $uri $uri/ /index.php?$query_string;
         }

 location ~ \.php(.*)$  {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }

     }
include vhosts.conf;

}

 

### 解析 Nginx 404 Not Found 错误 当服务器返回 `404 Not Found` 错误时,意味着客户端尝试访问的资源在服务器上不存在。对于Nginx而言,这通常是因为请求路径未能匹配任何实际存在的文件或未被正确转发给应用程序处理。 #### 文件位置不正确 如果直接访问域名如 app.test.cc,则请求会被导向至 `/home/app/login/jboss/webapps` 下查找默认首页文件(例如 index.jsp, index.htm 或者 index.html)。一旦这些指定的入口文件缺失于该目录内,便会触发404响应[^1]。 #### 路由配置不当 针对基于PHP框架的应用程序(比如Laravel),即使根路径可以成功加载,其他自定义路由可能会因为缺少必要的重写规则而遭遇404错误。特别是当试图隐藏index.php时,如果没有适当调整location块中的try_files指令来支持干净URL模式的话,就容易发生此类情况[^3]。 #### 配置文件语法错误 另外,在修改Nginx配置过程中引入了非法字符或是遗漏某些必要符号也会造成服务端解析失败并给出类似的提示信息。因此建议使用专业的文本编辑工具而非简单的记事本来编辑.conf文件以减少潜在的风险因素[^4]。 为了有效解决上述提到的各种原因引起的404问题: ```nginx server { listen 80; server_name localhost; root /path/to/your/project/public; # 确认此路径指向项目public目录 location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.x-fpm.sock; # 根据实际情况替换版本号 } } ``` 这段代码展示了如何通过设置正确的root路径以及利用try_files命令让所有未知URI最终都交由index.php处理的方式,从而避免因找不到具体文件而导致的404错误。同时确保fastcgi部分能够顺利传递PHP请求给后端解释器执行。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值