Nginx代理的方式(反向代理)


一:Nginx 正向代理與反向代理

1.什么是代理服務器?
所謂代理服務器就是位於發起請求的客戶端與原始服務器端之間的一台跳板服務器,正向代理可以隱藏客戶端,反向代理可以隱藏原始服務器
  • 應用:VPN
2.正向代理的概念
  • 正向代理:客戶端 <一> 代理 一>服務端
正向代理,是一個位於客戶端和原始服務器(origin server)之間的服務器,為來從原始服務器取得內容,客戶端向代理發送一個請求並指定目標(原始服務器),然后代理向原始服務器轉交請求並將獲得的內容返回給客戶端。

image

3. 反向代理的概念
客戶端 一>代理 <一> 服務端
反向代理正好相反,對於客戶端而言它就像是原始服務器,並且客戶端不需要進行任何特別的設置。客戶端向反向代理命名空間(name-space)中的內容發送普通請求,接着反向代理將判斷向何處(原始服務器)轉交請求,並將獲得的內容返回給客戶端,就像這些內容原本就是它自己的一樣。
  • 應用:負載均衡
    image

二:Nginx代理服務支持的協議

ngx_http_uwsgi_module		: Python
ngx_http_fastcgi_module		: PHP 
ngx_http_scgi_module		: Java
ngx_http_v2_module			: Golang
ngx_http_proxy_module		: HTTP

三:Nginx代理實踐

1.部署web01
[root@web01 conf.d]# cat game5.conf 
server {
    listen 80;
    server_name 192.168.15.7;
    location / {
        root /opt/Super_Marie;
	index index.html;
    }
    location ~ /images {
        root /opt/image;
    }
}
2.部署lb01
  • 部署Nginx
1.下載Nginx源代碼包
[root@lb01 ~]# wget https://nginx.org/download/nginx-1.20.2.tar.gz

2.解壓
[root@lb01 ~]# tar -xf nginx-1.20.2.tar.gz

3.進入源代碼目錄
[root@lb01 ~]# cd nginx-1.20.2

4.安裝依賴包
[root@lb01 nginx-1.20.2]# yum install openssl openssl-devel zlib zlib-devel -y

5.設置編譯參數
[root@lb01 nginx-1.20.2]# ./configure  --with-http_gzip_static_module    --with-stream     --with-http_ssl_module

6.編譯
[root@lb01 nginx-1.20.2]# make 

7.安裝
[root@lb01 nginx-1.20.2]# make install 

8.優化
cd /usr/local/nginx

[root@lb01 nginx]# mkdir /etc/nginx
mv conf/* /etc/nginx/

[root@lb01 nginx]# mkdir /etc/nginx/conf.d

9.配置nginx.conf文件
cd /etc/nginx/

1.web01服務器 
2.將etc/nginx/nginx.conf配置文件
3.復制到lb01 nginx.conf 內

[root@lb01 nginx]# vi nginx.conf
user  www;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $http_host - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
                  

四:lb01服務器部署Nginx

1.增加屬組 屬主
[root@lb01 nginx]# groupadd www -g 666
[root@lb01 nginx]# useradd www -u 666 -g 666 -M -r -s /sbin/nologin
2.增加配置文件
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"

[Install]
WantedBy=multi-user.target
3.移動
[root@lb01 sbin]# mv /usr/local/nginx/sbin/nginx /usr/sbin/
4.測試是否成功
/usr/sbin/nginx -h
5.重載
systemctl daemon-reload
6.創建
[root@lb01 sbin]# mkdir /var/log/nginx
7.測試
nginx -t
8.重啟
systemctl restart nginx

五:lb01部署反向代理

1.修改配置文件
[root@lb01 conf.d]# vim /etc/nginx/conf.d/game.conf 
server {
    listen 80;
    server_name _;
    location / {
        proxy_pass http://172.16.1.7:80;
    }
}
2.創建軟連接
[root@lb01 sbin]# ln -s /etc/nginx/nginx.conf /usr/local/nginx/conf/nginx.conf

3.測試
nginx -t
4.重啟
systemctl restart nginx
5.DNS域名解析
192.168.15.5
6.網址
192.168.15.5

image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM