刚apache转nginx,这个虚拟目录就是没法执行php,怎么搞?

ubuntu + nginx
http://localhost/正常
http://loacalhost/abc/不正常
按网上的教程,不是404,就是没法执行php(呈现php源码状态)

配置文件如下,该配置出现的是404:


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

    root /var/www/html;

    
    index index.php index.htm index.nginx-debian.html;

    server_name _;

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

    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;


    }
    
    location /abc {
        alias /home/abc/web/;
        
        index index.php;
    }
    
    location ~ ^/abc/(.*\.php)$ {
        alias /home/abc/web/$1;
        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;

    }

}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#    listen 80;
#    listen [::]:80;
#
#    server_name example.com;
#
#    root /var/www/example.com;
#    index index.php;
#
#    location / {
#        try_files $uri $uri/ =404;
#    }
#}
阅读 2.8k
2 个回答

需要调整一下Nginx配置,如下你参考一下,路径要自己对应好

location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
    include        fastcgi_params;

    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;
}

只需微调nginx的初始配置就可以,至于你用的 alias ,我这里没有用到。建议你这样配置。关键配置都写了中文注释。

nginx.conf

……
server
    {
        # 本地默认
        listen 127.0.0.1:80;
        server_name _;
        index index.html index.htm index.php;
        root  /home/wwwroot/default; # 路径设置到nginx服务跟目录

        include enable-php.conf;
        ……
        access_log  /home/wwwlogs/access.log;
    }

include vhost/*.conf; # 导入每个项目的独立配置

vhost/demo.conf

server
   {
        listen 80;
        #listen [::]:80 default_server ipv6only=on;
        server_name demo.vm.com; # 设置虚拟域名访问不同的项目
        index index.php index.htm index.html;
        root  /home/wwwroot/default/demo/public; # 设置到项目的入口文件所在目录

        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        # laravel 这里是每个框架的url解析规则,不同框架稍有不同
        location / {
           try_files $uri $uri/ /index.php?$query_string;
        }
        
        # include enable-php-pathinfo.conf;
        # 设置 nginx 转发到 php-fpm 或者也可以配置到另一个文件里,然后通过 include 引入
        location ~ [^/]\.php(/|$)
        {
            try_files $uri =404;
            fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
        }

        location /nginx_status
        {
            stub_status on;
            access_log   off;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
           allow all;
        }

        access_log  /home/wwwlogs/access.log;
    }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题