日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Nginx如何配置多個(gè)服務(wù)域名解析共用80端口詳解_nginx

作者:空靈之海 ? 更新時(shí)間: 2022-11-06 編程語言

前言

由于公司一臺(tái)服務(wù)器同時(shí)有多個(gè)服務(wù),這些服務(wù)通過域名解析都希望監(jiān)聽80/443端口直接通過域名訪問,比如有demo.test.com和product.test.com。這時(shí)候我們可以使用nginx的代理轉(zhuǎn)發(fā)功能幫我們實(shí)現(xiàn)共用80/443端口的需求。

備注:由于HTTP協(xié)議默認(rèn)監(jiān)聽80端口,HTTPS協(xié)議默認(rèn)監(jiān)聽443端口,所以使用瀏覽器訪問80/443端口的服務(wù)時(shí),可以忽略域名后的“ :80/:443” 端口,直接配置監(jiān)聽到80端口,訪問比較方便。

配置nginx多服務(wù)共用80端口

首先找到nginx配置文件 ? ?

通過apt-get install nginx命令安裝的nginx默認(rèn)配置文件存放在:/etc/nginx目錄下
 
切換到/etc/nginx目錄
 
#cd /etc/nginx           #切換到nginx目錄
 
# ls                     #查看nginx目錄下文件
conf.d        fastcgi_params  koi-win     modules-available  nginx.conf    scgi_params      sites-enabled  uwsgi_params fastcgi.conf  koi-utf         mime.types  modules-enabled    proxy_params  sites-available  snippets       win-utf
 
#vim nginx.conf          #打開nginx配置文件(輸入shift+i插入內(nèi)容,esc退出編輯,點(diǎn)擊shift+:輸入q退出當(dāng)前頁,q!強(qiáng)制退出,不保存編輯的內(nèi)容;輸入wq!強(qiáng)制退出并保存)

以下以兩個(gè)服務(wù)使用域名訪問,共用80端口為例

方案一:多個(gè)不同端口服務(wù)共用80端口

1)配置nginx.conf文件

1.先配置兩個(gè)端口服務(wù):
// nginx.conf
#demo
server {
    listen       8001;
    server_name localhost;
    try_files $uri $uri/ /index.html;
    root    /home/www/demo;
}
#product
server {
    listen        8002;
    server_name  localhost;
    try_files $uri $uri/ /index.html;
    root    /home/www/product;
}
 
2.配置代理:
// nginx.conf
#demo轉(zhuǎn)發(fā)
server {
    listen       80;
    server_name demo.test.com;
    location / {
        proxy_pass http://localhost:8001;
    }
}
#product轉(zhuǎn)發(fā)
server {
    listen       80;
    server_name product.test.com;
    location / {
        proxy_pass http://localhost:8002;
    }
}

2)配置完成后重啟nginx服務(wù)

#systemctl restart nginx

3) ?如果是本地局域網(wǎng)需要配置網(wǎng)絡(luò)將對(duì)應(yīng)的端口,我這邊是80,8001,8002三個(gè)端口映射到公網(wǎng)IP,并解析對(duì)應(yīng)的域名,完成后就可以正常訪問了;

方案二:多個(gè)服務(wù)共用80端口

1)配置nginx.conf文件

// nginx.conf
# nginx 80端口配置 (監(jiān)聽demo二級(jí)域名)
server {
    listen  80;
    server_name     demo.test.com;
    location / {
        root   /home/www/demo;
        index  index.html index.htm;
    }
}
 
# nginx 80端口配置 (監(jiān)聽product二級(jí)域名)
server {
    listen  80;
    server_name     product.test.com;
    location / {
        root   /home/www/product;
        index  index.html index.htm;
    }
}

2)參考方案一,配置完成后保存,重啟nginx服務(wù),訪問測試。

總結(jié)

原文鏈接:https://blog.csdn.net/weixin_44569100/article/details/126179849

欄目分類
最近更新