账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    nginx访问不存在的php文件
    29
    0

    访问a.com/pay.php nginx直接报错了 File not found. 因为我php yii2项目用了路由规则pay.php 会访问 a.com/pay/test/add 这个url.
    现在问题来了。可以我用了nginx 访问 a.com/pay.php 直接就报错了。还没走到我php项目来。。。

    以下是我的配置:

    server {
            listen       80;
            server_name a.com;
            index  index.php index.html index.htm;
            set $DOCROOT "/data/web/fr/frontend/web";
    
       
            location / {
                root   $DOCROOT;
                try_files $uri $uri/ /index.php?$args @rewrite
                index index.php;
    
                   if ($host ~* /pay.p a.com/pay/test/add/$1 permanent;
                }
    
              }
    
                 location ~* .*\.(php|php5)?$ {
                 root $DOCROOT;
                 fastcgi_pass  127.0.0.1:9000;
                 fastcgi_index index.php;
                 include fastcgi.conf;
    
              }
          }
    

    怎么配置都不好,希望大神帮助。

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 圈ぐ心ㄅ 普通会员 1楼

      在Nginx中,如果想要访问不存在的PHP文件,你需要在服务器配置文件中进行相应的处理。以下是一个基本的配置示例:

      ```nginx http { server { listen 80; server_name example.com;

          location / {
              if (!-e $document_root/index.php) {
                  root /var/www/example.com/public_html;
                  index index.php;
              }
          }
      }
      

      } ```

      在这个配置中,Nginx会监听80端口,然后根据域名访问对应的服务器。如果文件index.php不存在,Nginx会将请求重定向到服务器的根目录/var/www/example.com/public_html,并将index.php作为默认的主页。

      请注意,这个配置只适用于Apache服务器。在Nginx中,你需要将rootindex更改为适合你的服务器配置。

      此外,你也可以使用file_get_contents()函数来检查文件是否存在。例如:

      ```nginx http { server { listen 80; server_name example.com;

          location / {
              if (!file_get_contents($document_root/index.php)) {
                  root /var/www/example.com/public_html;
                  index index.php;
              }
          }
      }
      

      } ```

      在这个配置中,file_get_contents()函数会尝试打开index.php文件,并返回文件的内容。如果文件不存在,file_get_contents()函数会返回false,Nginx会将请求重定向到服务器的根目录。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部