nginx反向代理踩坑實戰(zhàn)記錄(容器方式)
一、簡述
1.1 什么是反向代理?
這很重要,反向代理就是代理服務器代理真實服務器??蛻舳艘詾榇矸掌骶褪钦鎸嵎掌鳎跃蜁岩埱蟮?=資源(URL)==發(fā)給代理服務器。
代理服務器一般是由nginx來充當,代理功能由配置文件來完成。

1.2 看圖理解
畫的倉促,大概有這個意思

1.3 錯誤總結
錯誤記錄:
nginx和tomcat全容器化
用nginx代理兩臺tomcat服務器,
當訪問資源帶edu的時候交給tomcat1
當訪問資源帶vod的時候交給tomcat2
做反向代理測試的時候,寫的是完整的URL,不是部分路徑。因為代理服務器在客戶端看來就是真實服務器?。。?/p>
這次對反向代理的理解更深刻了,堅持下來沒有錯。
同時,請教別人也是必須的,多交流多思考才能更好的解決問題~
以下是錯誤演示

二、正確案例
2.1 啟動nginx
docker run --name nginx -p 80:80 --link=tomcat:tomcat1 --link=tomcat02:tomca -v /opt/docker-nginx/nginx.conf:/etc/nginx/nginx.conf -v /opt/docker-nginx/log:/var/log/nginx -v /opt/docker-nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -d 313ec0a602bc
2.2 啟動tomcat
先啟動一個
docker run -it -p 8080:8080 tomcat
docker cp復制文件
docker cp 容器id:/usr/local/tomcat/webapps.dist/* /opt/webapps
再啟動掛載卷
docker run -it -p 8081:8080 --name tomcat01 -v /opt/webapps:/usr/local/tomcat/webapps tomcat docker run -it -p 8082:8080 --name tomcat02 -v /opt/webapps:/usr/local/tomcat/webapps tomcat
建文件,建資源
[root@VM-16-8-centos vod]# ll total 4 -rw-r–r-- 1 root root 11 Apr 14 21:42 a.html [root@VM-16-8-centos vod]# pwd /opt/tomcat/webapps/vod [root@VM-16-8-centos edu]# ll total 4 -rw-r–r-- 1 root root 8 Apr 14 21:26 a.html [root@VM-16-8-centos edu]# pwd /opt/tomcat/webapps/edu

2.3 配置nginx
[root@VM-16-8-centos docker-nginx]# vim nginx.conf
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 xxx.xxx.xxx.xxx;
location ~ /edu/ {
proxy_pass http://xxx.xxx.xxx.xxx:8081;
}
location ~ /vod/ {
proxy_pass http://xxx.xxx.xxx.xxx:8082;
}
}
}
2.4 重啟所有服務
docker restart …
2.5 測試


三、云服務器上跑的nginx怎么代理本地項目
不可以!
要么都在云端,只有代理服務器IP和真實服務器IP能互通的情況下才能代理!??!
總結
到此這篇關于nginx反向代理踩坑實戰(zhàn)的文章就介紹到這了,更多相關nginx反向代理踩坑內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
nginx如何根據(jù)報文里字段轉(zhuǎn)發(fā)至不同地址
要在 Nginx 中根據(jù) POST 請求的 JSON 負載中的 id 字段的值進行轉(zhuǎn)發(fā),你可以使用 Nginx 的 ngx_http_lua_module 模塊,這個模塊允許你在 Nginx 配置中使用 Lua 腳本,本文介紹nginx如何根據(jù)報文里字段轉(zhuǎn)發(fā)至不同地址,感興趣的朋友一起看看吧2024-12-12
Nginx rewrite和proxy_pass的區(qū)別及說明
這篇文章主要介紹了Nginx rewrite和proxy_pass的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
詳解Nginx反向代理實現(xiàn)會話(session)保持的兩種方式
這篇文章主要介紹了詳解Nginx反向代理實現(xiàn)會話(session)保持的兩種方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08
PHP的Symfony和CodeIgniter框架的Nginx重寫規(guī)則配置
這篇文章主要介紹了PHP的Symfony和CodeIgniter框架的Nginx重寫規(guī)則配置,文中截取配置中關鍵的一些rewrite寫法進行講解,需要的朋友可以參考下2016-01-01

