首頁  >  文章  >  運維  >  Nginx如何設定二級域名

Nginx如何設定二級域名

WBOY
WBOY轉載
2023-05-12 23:22:044390瀏覽

當一個域名需要使用在兩個項目上後,我們就需要使用到二級域名,在Nginx 中配置二級域名如下:

1、原始配置文件如下

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

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

}

這是解壓縮後的nginx.conf 文件,可以看出,當前nginx 監聽的是80 端口,它的服務名為localhost,假如我們的域名為:baidu.com,那我們輸入:localhost.baidu.com也是可以訪問的。

2、配置二級網域

對於我們剛才理解的服務名,假如我們的網域為:baidu.com,我們需要配置的二級網域為asurplus.baidu.com,我們的設定檔如下

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    
    server {
        listen       80;
        server_name  asurplus.baidu.com;

        location / {
            proxy_pass http://127.0.0.1:8081;
        }
    }

}

到sbin 目錄,執行指令重啟nginx

./nginx -s reload

我們新增了一個服務,監聽的依然是80 端口,我們的服務名稱變成了我們的二級域名:asurplus,並轉發到了我們的8081 端口,從而完成了二級域名的配置。

以上是Nginx如何設定二級域名的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除