Nginx 的两个不同目录

Nginx 的两个不同目录

我有两个目录directory1在我的服务器上,其他目录里面,还有directory2一个单个文件 index.php

我想访问我index.phphttp://服务器//var/www/directory2/index.php)并且能够directory1使用http://服务器/测试var/www/directory1/test) 例如。

我尝试了该配置,但无法访问该index.php文件:

server {
    server_name server;

    index index.php index.html index.htm;
    access_log /var/log/nginx/server.access.log;
    error_log /var/log/nginx/server.error.log;

    location / {
        root /var/www/directory1;
    }

    location ~ ^/index.php$ {
        root /var/www/directory2;
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param APPLICATION_ENV production;
        include fastcgi_params;
    }
}

谢谢你的帮助 !

答案1

我会做这个简单的:

root /var/www/directory1;

location = / {
    root /var/www/directory2;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root/index.php;
    fastcgi_param APPLICATION_ENV production;
    include fastcgi_params;
}

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_index index.php;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param APPLICATION_ENV production;
    include fastcgi_params;
}

相关内容