1. 程式人生 > >nginx 配置一個或者多個域名

nginx 配置一個或者多個域名

 nginx 配置一個或者多個域名

cd etc/nginx/ 進入到nginx目錄

vi nginx.conf

 server 一個為例

server {
    listen       80 default_server;
    #listen       [::]:80 default_server;
    server_name  www.***.com;
    root         /home/tt/public;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
                index  index.html index.htm index.php;
                #autoindex  on;

              if (!-e $request_filename) {
                rewrite  ^(.*)$  /index.php?s=/$1  last;
                break;
              }
            }
    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

    location ~ .*.php?$ {
        # 設定監聽埠
        fastcgi_pass   127.0.0.1:9000;
        # 設定nginx的預設首頁檔案(上面已經設定過了,可以刪除)
        fastcgi_index  index.php;
        #fastcgi_split_path_info ^(.+\.php)(.*)$;     #增加這一句
        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;    #增加這一句
        # 設定指令碼檔案請求的路徑
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        #fastcgi_param  SCRIPT_FILENAME  /home$fastcgi_script_name;
        # 引入fastcgi的配置檔案
        include        fastcgi_params;
    }
}

新增多個複製放到下方繫結不同的域名和專案路徑即可

server {
     listen       80;
     #listen       [::]:80 default_server;
     server_name  www.xxx.com;
     root         /home/new/public;

     # Load configuration files for the default server block.
     include /etc/nginx/default.d/*.conf;

     location / {
                 index  index.html index.htm index.php;
                 #autoindex  on;

               if (!-e $request_filename) {
                 rewrite  ^(.*)$  /index.php?s=/$1  last;
                 break;
               }
             }
     error_page 404 /404.html;
         location = /40x.html {
     }

     error_page 500 502 503 504 /50x.html;
         location = /50x.html {
     }

     location ~ .*.php?$ {
         # 設定監聽埠
         fastcgi_pass   127.0.0.1:9000;
         # 設定nginx的預設首頁檔案(上面已經設定過了,可以刪除)
         fastcgi_index  index.php;
         #fastcgi_split_path_info ^(.+\.php)(.*)$;     #增加這一句
         fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
         fastcgi_param PATH_INFO $fastcgi_path_info;    #增加這一句
         # 設定指令碼檔案請求的路徑
         fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
         #fastcgi_param  SCRIPT_FILENAME  /home$fastcgi_script_name;
         # 引入fastcgi的配置檔案
         include        fastcgi_params;
     }
 }

配置完成後記得重新啟動nginx

service nginx restart