具有多个位置块的Nginx配置 [英] nginx configuration with multiple location blocks

查看:68
本文介绍了具有多个位置块的Nginx配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to configure nginx to serve 2 different php scripts from 2 different location. The configuration is as follows.

  1. 我有一个Laravel安装,该安装位于/home/hamed/laravel中,在其中应提供其public目录.
  2. 我在/home/hamed/www/blog中安装了Wordpress.
  1. I have a Laravel installation which resides in /home/hamed/laravel in which its public directory should be served.
  2. I have a Wordpress installation in /home/hamed/www/blog.

这是我的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安装仍会接管请求.

The problem is when trying to access the wordpress section by calling example.com/blog still the the laravel installtion takes over the request.

现在,我尝试将location块内的root指令替换为alias无济于事.

Now I have tried replacing root directive inside location blocks with alias to no avail.

根据本指南location中包含index指令或try_files会触发内部重定向,我怀疑会导致此行为.

According to this guide having the index directive or try_files inside location triggers an internal redirect which I suspect causes this behavior.

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

Would someone please help me figure this out?

推荐答案

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

Thanks to @RichardSmith I finally managed to create the right configuration. Here is the final working config. I had to use the combination of nested location blocks and an inverse regex match for it to work.

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


}

这篇关于具有多个位置块的Nginx配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆