首页 > 解决方案 > nginx try_files php file is not parsed

问题描述

My project was split into three projects for historical reasons

This makes the nginx configuration file exceptionally difficult to handle, so much that I have tried many solutions and still haven't solved it perfectly.

Directory configuration:

The configuration file is as follows:


server {
    listen   80;
    listen   [::]:80 default ipv6only=on;

    server_name _;
    root  /var/www/html;
    index index.html index.htm index.php;

    sendfile off;

    location / {
        try_files $uri $uri/index.html $uri/index.htm $uri/index.php @mnt;
        # try_files $uri $uri/ @mnt;
        # This annotated code causes a 403 error to be returned when accessing an empty directory instead of redirecting to @mnt

    }

    location @mnt {
        root /mnt/a.com;
        try_files $uri $uri/index.html $uri/index.htm $uri/index.php @proxy;
    }

    location @proxy {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://a-proxy.com;
    }


    location ~ \.php$ {
        try_files $uri @mntphp;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location @mntphp {
        root /mnt/a.com;
        try_files $uri @proxy;

        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    # deny access to . files, for security
    location ~ /\. {
        log_not_found off;
        deny all;
    }

    location ~* \.(jpg|jpeg|gif|png|css|js|ico|webp|tiff|ttf|svg)$ {
        expires 2d;
    }

    error_log   /var/log/a.com_error.log;
    access_log  /var/log/a.com.log;
}

But such a configuration will cause the php file to fail to parse. I know this is because try_files will redirect only the last parameter, but I don't know how to solve this

标签: phpnginx

解决方案


推荐阅读