1. 程式人生 > >mac 下 nginx 配置多個虛擬主機

mac 下 nginx 配置多個虛擬主機

我是通過homebrew 安裝nginx 的,所以安裝目錄是預設的,之前多個server都是放在預設安裝目錄下的nginx.conf裡的,但是這樣不太好,就是會導致nginx.conf 越來越長,而且容易出現一些大括號缺失啥的類似的語法錯誤。看了下老大配的虛擬目錄感覺不錯,自己弄了下,成功了。好處是每個server 站點相互獨立,互不影響,很簡單,分享下過程~

    1、首先在nginx 的配置目錄下:/usr/local/etc/nginx 新建一個資料夾sites,然後可以建立一個或多個配置檔案例如nginx-test.conf。

    2、新增server的配置檔案。

    server {
        listen       80;  //為了訪問時不用寫埠號,我把mac 的nginx監聽埠改成80埠了
        server_name  www.test.com;
        index index.html index.htm index.php;
        root /Usrs/ad/www/tp5/public; #這是我測試的tp5安裝目錄
        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000; #/run/php/php5.6-fpm.sock
                fastcgi_index  index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
                try_files $uri = 404;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
                expires 30d;
        }
        location ~ .*\.(js|css)?$
        {
                expires 1h;
        }

        ###this is to use open website lianjie like on apache##
        location / {
                if (!-e $request_filename) {
                        rewrite ^(.*)$ /index.php?s=$1 last;
                        break;
                }
        }
        ###end##
        access_log  /var/log/nginx/access/tp5.log main;

}

3、在配置檔案nginx.conf http 下新增 include sites/nginx-*.conf.

 4、在/etc/hosts 裡增加

   127.0.0.1   www.test.com

  5、sudo nginx -s reload 重啟nginx 

  6、訪問www.test.com 就可以看到tp5 頁面的笑臉了,成功~