【问题标题】:Serving php files from different locations in nginx在 nginx 中从不同位置提供 php 文件
【发布时间】:2011-01-15 06:47:52
【问题描述】:

我的主网站和 wordpress 在我使用 nginx 作为 Web 服务器的服务器上的不同目录中。主网站位于 /home/me/www 中,而 Wordpress 位于 /home/me/wordpress 中。出于特定原因,我需要以这种方式将它们放在单独的目录中。如何在 nginx 配置文件中指定这个?我目前有以下,但它不起作用:

location / {
    root   /home/me/www;
    index  index.php index.html index.htm;
}

location /blog {
    root /home/me/wordpress;
    index index.php index.html index.htm;
}

location ~ \.php$ {
    set $php_root /home/me/www;
    if ($request_uri ~ /blog) {
        set $php_root /home/me/wordpress;
    }
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $php_root$fastcgi_script_name;
    include /etc/nginx/fastcgi_params;
}

当我尝试访问 http://mydomain/blog 时,它当前返回 HTTP 404

【问题讨论】:

    标签: php configuration nginx


    【解决方案1】:

    查看this questionNginx Manual

    尝试将您的 blog 行更改为:

    location ^~ /blog/ {
        root /home/me/wordpress;
        index index.php index.html index.htm;
    }
    

    【讨论】:

    • 感谢您的回答!实际上,您指出的问题是我很久以前提出的问题!根据答案,我从来没有让它工作。直到今天我才开始工作。我已经对该答案发表了评论。
    【解决方案2】:

    我为此苦苦挣扎了几个小时,终于达到了如下的工作配置:

    location /php-app {
        passenger_enabled off;
        alias /path/to/php-app/$1;
        index index.php index.html;
        try_files $uri $uri/ @rewrite;
    }
    
    location @rewrite {
        rewrite ^/php-app(.*)$ /index.php?q=$1 last;
    }
    
    location ~ \.php$ {
        alias /path/to/php-app/$1;
        rewrite ^/php-app(.*)$ $1 last;
        passenger_enabled off;
        fastcgi_pass unix:/tmp/php-fpm.socket;
        fastcgi_index index.php;
        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /path/to/php-app$fastcgi_script_name;
        fastcgi_intercept_errors on;
    }
    

    【讨论】:

      【解决方案3】:

      只是在@Nick Presta 答案中添加更多细节。

      ^~:如果存在克拉和波浪号修饰符,并且如果该块被选为最佳非正则表达式匹配,则不会进行正则表达式匹配。

      检查这些差异enter link description here

      【讨论】:

        猜你喜欢
        • 2014-01-26
        • 2017-06-20
        • 2018-12-08
        • 2020-10-11
        • 1970-01-01
        • 2020-05-21
        • 2016-10-25
        • 1970-01-01
        • 2021-01-30
        相关资源
        最近更新 更多