網站首頁 編程語言 正文
前言
由于公司一臺服務器同時有多個服務,這些服務通過域名解析都希望監聽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
相關推薦
- 2023-03-29 python中文字符如何轉url編碼_python
- 2022-07-13 常用類之BigDecimal、Date、Calender、SimpleDateFormat及Syst
- 2022-01-01 stopPropagation是什么意思以及什么是事件的原理
- 2022-09-20 linux?shell字符串截取的詳細總結(實用!)_linux shell
- 2022-01-19 webpack5 熱更新無響應
- 2022-08-14 git?stash(儲藏)的用法總結_相關技巧
- 2022-04-25 Entity?Framework?Core中執行SQL語句和存儲過程的方法介紹_實用技巧
- 2023-05-19 Kotlin?this關鍵字的使用實例詳解_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支