Nginx-http反向代理-配置示例

Nginx-学习目录

1、环境准备

1.1、主机清单

192.168.10.4     nginx主机
192.168.10.5     web01 

1.2、反向代理架构图

user -> nginx -> web01

1.3、web01准备

1.3.1、准备测试代码

mkdir /opt/web01
echo "web-01" >/opt/web01/index.html

1.3.2、配置nginx

cat >/etc/nginx/conf.d/web01.cyc.com.conf<<'EOF'
server{
  listen 8080;
  server_name web01.cyc.com;
  location / {
    root /opt/web01;
    index index.html;
  }
}
EOF

1.3.3、重新加载nginx并且测试

systemctl restart nginx
~]# curl localhost:8080 web-01

2、配置反向代理nginx

2.1、配置常用的代理请求头参数

cat >/etc/nginx/proxy_params<<'EOF'
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
proxy_buffering on;
proxy_buffer_size 64k;
proxy_buffers 4 64k;
EOF

# 参考文章:https://www.cnblogs.com/ygbh/p/17365450.html#_label5

2.2、配置nginx

cat >/etc/nginx/conf.d/proxy_web01.cyc.com.conf<<'EOF'
server{
  listen 80;
  server_name web01.cyc.com;
  location / {
    proxy_pass http://192.168.10.5:8080;
    include proxy_params;
  }
}
EOF

2.3、重新加载nginx

systemctl reload nginx

2.4、配置hosts

echo "192.168.10.4 web01.cyc.com" >>/etc/hosts

2.5、测试访问

[root@nfs ~]# curl web01.cyc.com
web-01

2.6、完成配置

配置反理代理成功

 

posted @ 2023-04-30 18:21  小粉优化大师  阅读(103)  评论(0编辑  收藏  举报