目錄
- 一、代理服務原理
- 二、nginx配置代理的語法
- 三、nginx反向代理演示示例
一、代理服務原理
- 參考lz此博文:https://wwwxz.blog.csdn.net/article/details/119277923
二、nginx配置代理的語法
- Syntax:proxy_pass URL; URL表示所有客戶端請求請求到nginx代理服務器后,nginx代理服務器向原始服務器請求的url,URL格式:http://ip+埠/路徑
- Default:—— 表示默認沒有配置;
- Context:location; 表示需要在location塊中;
三、nginx反向代理演示示例
1、配置nginx訪問html頁面的路徑
-
創建nginxproxy.html頁面,上傳到/opt/app/html目錄下
<html lang="en"> <head> <meta charset="utf-8"> <title>測驗nginx反向代理</title> </head> <body style="background-color:red;"> <h1>nginx反向代理!!!!</h1> </body> </html>
2、編輯 nginx.conf 組態檔可以看到在/etc/nginx/conf.d/目錄下可以創建子組態檔,如下圖:

3、lz在/etc/nginx/conf.d/目錄下創建一個origin_server.conf(原始服務組態檔)和proxy_server.conf(代理服務組態檔),內容分別如下:
-
origin_server.conf組態檔內容:(注:因為原始服務用的是8080埠,需要配置8080埠不被外界所訪問)
server { listen 8080; server_name localhost; location / { root /opt/app/html; index index.html index.htm; } error_page 404 /404.html; location = /404.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } }
-
proxy_server.conf組態檔內容如下:
server { listen 80; #埠 server_name localhost; #訪問的ip location / { root /opt/app/html; index index.html index.htm; } #配置代理服務器訪問原始服務器的地址 location ~ /nginxproxy.html$ { proxy_pass http://localhost:8080; } error_page 404 /404.html; location = /404.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } }
4、啟動nginx服務并檢查組態檔
-
啟動nginx服務
[root@localhost conf.d]# systemctl start nginx.service -
檢查配置修改的組態檔是否正確,回傳successful表示組態檔修改無錯
[root@localhost nginx]# nginx -t -c /etc/nginx/nginx.conf
5、重新加載nginx組態檔,并查看
[root@localhost conf.d]# nginx -s reload -c /etc/nginx/nginx.conf
6、查看本機啟用nginx的埠
[root@localhost conf.d]# netstat -luntp|grep nginx

7、在瀏覽器輸入http://localhost/nginxproxy.html地址(即通過代理服務的80埠訪問,可以訪問到頁面),如下圖:

8、在瀏覽器輸入http://localhost:8080/nginxproxy.html地址(即直接訪問原始服務的8080埠訪問,不可以訪問到頁面),如下圖:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/291743.html
標籤:其他
上一篇:Llinux作業系統基礎------9.linux系統中的虛擬化部署
下一篇:Docker網路
