【问题标题】:nginx configuration with multiple location blocks具有多个位置块的 nginx 配置
【发布时间】:2015-12-04 07:12:44
【问题描述】:

我正在尝试将 nginx 配置为从 2 个不同的位置提供 2 个不同的 php 脚本。配置如下。

  1. 我有一个 Laravel 安装,它位于 /home/hamed/laravel 中,应该在其中提供其 public 目录。
  2. 我在/home/hamed/www/blog 中安装了 Wordpress。

这是我的nginx 配置:

server {
        listen  443 ssl;
        server_name example.com www.example.com;

        #root /home/hamed/laravel/public;

        index index.html index.htm index.php;

        ssl_certificate /root/hamed/ssl.crt;
        ssl_certificate_key /root/hamed/ssl.key;

        location /blog {
                root /home/hamed/www/blog;
                try_files $uri $uri/ /blog/index.php?do=$request_uri;
        }

        location / {
                root /home/hamed/laravel/public;
                try_files $uri $uri/ /index.php?$request_uri;
        }
        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                #fastcgi_pass 127.0.0.1:9000;
                fastcgi_pass unix:/var/run/php5-fpm.hamed.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

}

问题是当尝试通过调用example.com/blog 访问 wordpress 部分时,laravel 安装仍然接管了请求。

现在我尝试用 alias 替换 location 块内的 root 指令,但无济于事。

根据this guidelocation 中包含index 指令或try_files 会触发内部重定向,我怀疑这会导致此行为。

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: php nginx nginx-location


    【解决方案1】:

    问题在于location ~ \.php$ { ... } 负责处理您的所有 php 脚本,这些脚本分为两个不同的根。

    一种方法是对server 容器使用通用root,并在每个前缀位置块内执行内部重写。比如:

    location /blog {
      rewrite ^(.*\.php)$ /www$1 last;
      ...
    }
    location / {
      rewrite ^(.*\.php)$ /laravel/public$1 last;
      ...
    }
    location ~ \.php$ {
      internal;
      root /home/hamed;
      ...
    }
    

    以上应该可以工作(但我没有用你的场景测试过)。

    第二种方法是使用嵌套的位置块。然后将location ~ \.php$ { ... } 块复制到每个应用程序的位置块中。比如:

    location /blog {
      root /home/hamed/www;
      ...
      location ~ \.php$ {
        ...
      }
    }
    location / {
      root /home/hamed/laravel/public;
      ...
      location ~ \.php$ {
        ...
      }
    }
    

    现在已经过测试。

    【讨论】:

    • 谢谢,虽然你的解决方案实际上并没有奏效,但它让我找到了正确的解决方案,我很快就会在这里发布。
    • 我的第二种方法中有一个错字:root /home/hamed/www/blog; 应该是root /home/hamed/www;。也许这就是它不起作用的原因。反正我已经修好了。
    【解决方案2】:

    感谢@RichardSmith,我终于设法创建了正确的配置。这是最终的工作配置。我必须使用嵌套的 location 块和反向正则表达式匹配的组合才能工作。

    server {
        listen  443 ssl;
            server_name example.com;
            root /home/hamed/laravel/public;
    
    #        index index.html index.htm index.php;
    
            ssl_certificate /root/hamed/ssl.crt;
            ssl_certificate_key /root/hamed/ssl.key;
    
            location ~ ^/blog(.*)$ {
            index index.php;
            root /home/hamed/www/;
            try_files $uri $uri/ /blog/index.php?do=$request_uri;
    
            location ~ \.php$ {
                        try_files $uri =404;
                        fastcgi_split_path_info ^(.+\.php)(/.+)$;
                        #fastcgi_pass 127.0.0.1:9000;
                        fastcgi_pass unix:/var/run/php5-fpm.hamed.sock;
                        fastcgi_index index.php;
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        include fastcgi_params;
                }
    
            }
    
        location ~ ^((?!\/blog).)*$ { #this regex is to match anything but `/blog`
            index index.php;
                    root /home/hamed/laravel/public;
                    try_files $uri $uri/ /index.php?$request_uri;
            location ~ \.php$ {
                        try_files $uri =404;
                        fastcgi_split_path_info ^(.+\.php)(/.+)$;
                        #fastcgi_pass 127.0.0.1:9000;
                        fastcgi_pass unix:/var/run/php5-fpm.hamed.sock;
                        fastcgi_index index.php;
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        include fastcgi_params;
                }
            }
    
    
            error_page 404 /404.html;
            error_page 500 502 503 504 /50x.html;
            location = /50x.html {
                    root /usr/share/nginx/html;
            }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-07-31
      • 1970-01-01
      • 2014-02-12
      • 2017-11-12
      • 2018-10-25
      • 2017-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多