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

學無先后,達者為師

網站首頁 編程語言 正文

Nginx如何配置多個服務域名解析共用80端口詳解_nginx

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

前言

由于公司一臺服務器同時有多個服務,這些服務通過域名解析都希望監聽80/443端口直接通過域名訪問,比如有demo.test.com和product.test.com。這時候我們可以使用nginx的代理轉發功能幫我們實現共用80/443端口的需求。

備注:由于HTTP協議默認監聽80端口,HTTPS協議默認監聽443端口,所以使用瀏覽器訪問80/443端口的服務時,可以忽略域名后的“ :80/:443” 端口,直接配置監聽到80端口,訪問比較方便。

配置nginx多服務共用80端口

首先找到nginx配置文件 ? ?

通過apt-get install nginx命令安裝的nginx默認配置文件存放在:/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插入內容,esc退出編輯,點擊shift+:輸入q退出當前頁,q!強制退出,不保存編輯的內容;輸入wq!強制退出并保存)

以下以兩個服務使用域名訪問,共用80端口為例

方案一:多個不同端口服務共用80端口

1)配置nginx.conf文件

1.先配置兩個端口服務:
// 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轉發
server {
    listen       80;
    server_name demo.test.com;
    location / {
        proxy_pass http://localhost:8001;
    }
}
#product轉發
server {
    listen       80;
    server_name product.test.com;
    location / {
        proxy_pass http://localhost:8002;
    }
}

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

#systemctl restart nginx

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

方案二:多個服務共用80端口

1)配置nginx.conf文件

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

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

總結

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

欄目分類
最近更新