nginx配置多個站點的方法

這裏以配置2個站點對應2個不一樣域名爲例

操做環境:ubuntu 16.04 64位 nginx/1.10.3

假設:
IP地址: 111.111.111.111
域名1 example1.com 放在 /www/example1
域名2 example2.com 放在 /www/example2

配置 的基本思路和步驟以下:
1.把2個站點 example1.com, example2.com 放到 nginx 能夠訪問的目錄 /www/
2.給每一個站點分別建立一個 nginx 配置文件 example1.com.conf,example2.com.conf,
並把配置文件放到 /etc/nginx/sites-available/下,
3.把/etc/nginx/sites-available/的example1.com.conf,example2.com.conf兩個文件,軟鏈接到/etc/nginx/sites-enabled/目錄下
4.確認在 /etc/nginx/nginx.conf 裏面有一句php

 include /etc/nginx/sites-enabled/*;

這樣就會自動引用sites-enabled目錄下的全部配置文件。通常會有,若沒有這句,就手動加一下。

ps:至於爲嘛要搞個連接,而不直接把文件放到sites-enabled下,默認的default配置文件就是如此,你看sites-enabled下的default文件的屬性:html

lrwxrwxrwx 1 root root 34 Dec 23 22:09 default -> /etc/nginx/sites-available/default

就是個軟鏈接,源文件在sites-available下。因此這個地方,咱們也遵循這個原則。

5.去掉/etc/nginx/nginx.conf中的server_names_hash_bucket_size 前的註釋,並修改數值爲32的整數,例如128
server_names_hash_bucket_size 128;

6.最後重啓 nginxnginx

說明:
在example1.com.conf的配置文件中,server_name對應訪問的域名,配置成功後,在域名管理頁面,添加對應的域名記錄,指向服務器的IP。

若運行後報錯:Nginx: could not build the server_names_hash ,檢查步驟5 的設置。

example1.com.conf example2.com.conf配置文件示例:ubuntu

server {
        listen  80;
        server_name  example1.com www. example1.com;
        access_log  /www/access_ example1.log  main;
        location / {
            root   /www/example1.com;
            index  index.php index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
       # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ .php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /www/example1.com/$fastcgi_script_name;
            include        fastcgi_params;
        }
        location ~ /.ht {
            deny  all;
        }
}
相關文章
相關標籤/搜索