1. 程式人生 > >Linux Nginx 配置/繫結域名

Linux Nginx 配置/繫結域名

Linux Nginx 配置/繫結域名

1、遠端連線伺服器

2、Nginx配置域名

執行命令 vi /etc/nginx/conf.d/**.conf 開啟 Nginx 服務配置檔案

2.1、為每一個域名建立一個單獨的配置檔案時輸入以下內容:

    server
    {
	    listen   80;                            #監聽埠設為 80。
	    server_name  www.server110.com;         #繫結您的域名。
	    index index.htm index.html index.php;   #指定預設檔案。
	    root /home/www/server110.com;           #指定網站根目錄。
	    include location.conf;                  #當您需要呼叫其他配置檔案時才貼上此項,如無需要,請刪除此項。
    }

2.2、將多個域名規則寫進一個共同的配置檔案時輸入以下內容:

    server
    {
	    listen   80;                            #監聽埠設為 80。
	    server_name  www.server110.com;         #繫結您的域名。
	    index index.htm index.html index.php;   #指定預設檔案。
	    root /home/www/server110.com;           #指定網站根目錄。
	    include location.conf;                  #當您需要呼叫其他配置檔案時才貼上此項,如無需要,請刪除此項。
    }
    server
    {
	    listen   80;                            #監聽埠設為 80。
	    server_name  www.server111.com;         #繫結您的域名。
	    index index.htm index.html index.php;   #指定預設檔案。
	    root /home/www/server111.com;           #指定網站根目錄。
	    include location.conf;                  #當您需要呼叫其他配置檔案時才貼上此項,如無需要,請刪除此項。
    }

2.3、為無 WWW 字首的域名配置規則並加 301 跳轉時輸入以下內容:

    server
    {
	    listen 80;
	    server_name server110.com;
	    rewrite ^/(.*) http://www.server110.com/$1 permanent;
    }

2.4、需要為域名新增 404 提示時輸入以下內容:

    server
    {
	    listen   80;                            #監聽埠設為 80。
	    server_name  www.server110.com;         #繫結您的域名。
	    index index.htm index.html index.php;   #指定預設檔案。
	    root /home/www/server110.com;           #指定網站根目錄。
	    include location.conf;                  #當您需要呼叫其他配置檔案時才貼上此項,如無需要,請刪除此項。
	    error_page 404			    #/404.html;
    }

3、後續操作

  • 按 Esc 退出編輯並輸入 :wq 儲存退出。

  • 執行命令 nginx -t 檢查配置是否有誤,並按照報錯提示修復錯誤。

  • 執行命令 systemctl restart nginx 重啟 Nginx 服務。

  • 執行命令 systemctl reload nginx 重新載入 Nginx 服務。