36-nginx反向代理

36-nginx反向代理

Nginx常见问题:
按需修改:
1.图片大于1M以上上传限制问题:
vim /etc/nginx/nginx.conf

    client_max_body_size 20M;
    client_body_buffer_size 16k;

    #gzip  on;
    #charset utf-8,gbk;
"/etc/nginx/nginx.conf" 34L, 732C written                                     
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl restart nginx
修改php配置:
[root@web01 ~]# egrep 'max_filesize|max_file_upload|post_max' /etc/php.ini
post_max_size = 20M
upload_max_filesize = 20M
max_file_uploads = 20
[root@web01 ~]# systemctl restart php-fpm

Nginx反向代理
SNAT DNAT介绍
SNAT(Source Network Address Translation)和DNAT(Destination Network Address Translation)是Linux 中的两种网络地址转换技术,用于在网络数据包传输过程中修改源地址和目的地址。它们在网络环境中起到重要作用;允许在不同网络中进行网络通信,并提供网络安全和负载均衡等功能。
	1.SNAT(Source NAT):
		SNAT是一种将源IP地址和端口转换为不同的地址和端口的技术。当网络中内部设备(例如私网IP地址)需要访问外部网络(例如公共互联网)时,SNAT可以用于*将内部设备的源地址转换为一个公共IP地址*。这使外部服务器返回的数据包能够正确路由会内部网络。
	2.DNAT(Destination NAT):
		DNAT是一种将目标IP地址和端口转换为不同地址和端口的技术。当外部网络中的数据包需要访问内部网络时,DNAT可以用于将*外部请求的目标地址转化为内部网络中的目标地址*以确保数据包被正确路由到内部服务器。典型的用例包括将外部网络请求的流量引导到内部的服务器,例如将公共IP地址上的访问映射到局域网中的特定服务器。

1

正向代理与反向代理的区别:
1.区别在于形式上服务的“对象”不一样
2.正向代理代理的对象是客户端,为客户端服务
3.反向代理代理的对象是服务端,为服务器端服务
Nginx代理服务支持协议
Nginx作为代理服务,可以支持的代理协议非常多,具体如图下:

2

反向代理使用协议
如果将Nginx作为反向代理服务,常常会用到如下几种代理协议,如下图示:

在这里插入图片描述

反向代理模式与Nginx代理模块总结:

反向代理模式Nginx配置模块
http、websocket、httpsngx_http_proxy_module
fastcgingx_http_fastcgi_module
uwsgingx_http_uwsgi_module
grpcngx_http_v2_module
Nginx反向代理配置语法
Syntax:    proxy_pass URL;
Default:    —
Context:    location, if in location, limit_except

http://localhost:8000/uri/
http://192.168.56.11:8000/uri/
http://unix:/tmp/backend.socket:/uri
Nginx反向代理配置
web01和web02配置静态页面
web01:
[root@web01 conf.d]# cat static.conf 
server{
	listen 80;
	server_name www.static.com;

	location / {
	root /code/test;
	index index.html;
	}	


}
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx
[root@web01 conf.d]# mkdir /code/test -p
[root@web01 conf.d]# echo web01... > /code/test/index.html

Windows配置hosts解析
10.0.0.7 www.static.com

web02:
[root@web02 conf.d]# cat static.conf 
server{
	listen 80;
	server_name www.static.com;

	location / {
	root /code/test;
	index index.html;
	}	


}
[root@web02 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 conf.d]# systemctl restart nginx
[root@web02 conf.d]# mkdir /code/test -p
[root@web02 conf.d]# echo web01... > /code/test/index.html

Windows配置hosts解析
10.0.0.8 www.static.com
配置LB01的反向代理
1.准备一台LB01服务器 10.0.0.5
2.拷贝nginx仓库
scp /etc/yum.repos.d/nginx.repo /etc/yum.repos.d/php.repo root@10.0.0.5:/etc/yum.repos.d/
3.安装nginx
yum -y install nginx
4.配置nginx反向代理
[root@lb01 conf.d]#cat default.conf 
server{
	listen 80;
	server_name www.static.com;

	location / {
	proxy_pass http://10.0.0.7;
	include proxy_params;	
	}
}
[root@lb01 conf.d]#cat ../proxy_params 
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
[root@lb01 conf.d]#nginx -t 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 conf.d]#systemctl restart nginx

windows配置hosts解析到10.0.0.5 www.static.com
浏览器访问10.0.0.5
负载均衡配置
10.0.0.5
1.配置nginx仓库
2.安装nginx
3.配置负载均衡
[root@lb01 conf.d]#cat default.conf 
upstream webs {
	server 10.0.0.7;
	server 10.0.0.8;
}
server{
	listen 80;
	server_name www.static.com;

	location / {
	proxy_pass http://webs;
	include proxy_params;	
	}
}
[root@lb01 conf.d]#cat ../proxy_params 
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
[root@lb01 conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 conf.d]#systemctl restart nginx

测试:Windows访问www.static.com
  • 11
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

atomLg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值