Nginx基本配置内容

http 模块适用于处理 Web 请求,而 stream 模块适用于处理非 HTTP 流量,如数据库连接、邮件传输等。

在 stream 模块中,你可以配置一些 TCP 或 UDP 的代理服务,以便 Nginx 能够转发这些流量。

与网站相关的

关于网站相关的要写在http中

bash 复制代码
http {
	server {
		listen 80;
		server_name localhost;
		
		location / {
			root html;
			index index.html index.htm;	
			}
	}
}

创建私钥,公钥

bash 复制代码
openssl genrsa >cert.key
openssl req -x509 -key cert.key >cert.pem
#根据提示输入信息
curl -k https://sxxxx

地址重写

rewrite /a.html /b.html

这里第一个的/即表示匹配的意思 ,第二个/即表示网站里的/也就是下面location里的html

redirect 临时重定向

permanent 永久重定向301

last 不在读其他rewrite,但是管不着localtion里的rewrite

break不在读其他rewrite

bash 复制代码
http {
	server {
		listen 80;
		server_name localhost;
		#rewrite ^/a\.html$ /b.html redirect;
		#rewrite / http://www.baidu.com;
		#rewrite /(.*) http://www.baidu.com/$1;
		#if ($http_user_agent ~* firefox){
			#rewrite /(.*) firefox/$1
				#}
		#rewrite ^/a\.html$ /b.html permanent;
		location / {
			root html;
			index index.html index.htm;	
			}
	}
}

集群代理

这里的upstream 就是创建一个集群

下面的location里写的proxy_pass 就是调用上面创建的集群

proxy_pass指令允许你指定Nginx转发请求的后端服务器

使用upstream块是在有多个后端服务器或者想要进行负载均衡时的一种常见做法。然而,如果只有一个后端服务器,直接在server块的location块中使用

ip_hash 相当于会话保持

down 虽然说注释掉和down效果一样,但是不专业

bash 复制代码
upstream example-name {
	ip_hash #相同客户端访问相同服务器
	server 192.168.1.1:80;# 如果配置权重 健康检查添加 weight=3 max_fails=2 fail_timeout=30(默认10s)
	server 192.168.1.2:80;# 当某台服务器要维修停机的时候任务就不会分配到他上 down
}
	server {
		listen 80;
		server_name localhost;
		
		location / {
			proxy_pass http://example-name;
			root html;
			index index.html index.htm;	
			}
		}

四层代理

跳出http范围,也能启用一个你想监听的端口,以下用Custom_Port表示。

以下是带负载均衡的示例

bash 复制代码
stream {
	upstream example_name {
		server 192.168.1.1:80;
		server 192.168.1.2:80;
	}
	server {
		liscen Custom_Port;
		proxy_pass example_name;
	}
}

上面的也可以写为不带负载均的示例

bash 复制代码
stream {
	server {
		liscen coums_port;
		proxy_pass upstream_balancer; #负载均衡的地址
	}
}

ab测试

bash 复制代码
ab -n 1000 -c 10 http://example.com/
# -n表示发起总共1000个请求
# -c 10: 并发数为 10,即同时发起 10 个请求

常用的 proxy_set_header 参数及其作用

proxy_set_header Host $host;

将客户端请求的原始主机头信息传递给后端服务器。在许多情况下,这是有用的,特别是在后端服务器需要知道客户端请求的原始主机名时。

proxy_set_header X-Real-IP $remote_addr;

将客户端的真实 IP 地址传递给后端服务器。这对于在后端服务器上获取客户端的真实 IP 地址是有用的,尤其是当 Nginx 位于代理层时。

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

将客户端的原始 IP 地址传递给后端服务器。类似于上一个选项,它也用于获取客户端的真实 IP 地址。

proxy_set_header X-Forwarded-Proto $scheme;

将客户端请求的协议传递给后端服务器。这对于后端服务器判断请求是通过 HTTP 还是 HTTPS 进行的是有用的。

相关推荐
灯火不休ᝰ1 小时前
[win7] win7系统的下载及在虚拟机中详细安装过程(附有下载文件)
linux·运维·服务器
加油,旭杏4 小时前
【中间件学习】fastCG介绍和使用
学习·nginx·fastcgi
HHoao4 小时前
Ubuntu启动后第一次需要很久才能启动GTK应用问题
linux·运维·ubuntu
小灰兔的小白兔4 小时前
【Ubuntu】Ubuntu常用命令
linux·运维·ubuntu
winds~4 小时前
ubuntu中软件的进程管理-结束软件运行
linux·运维·ubuntu
bush45 小时前
使用root账号ssh登录虚拟机ubuntu
运维·ubuntu·ssh
叫我龙翔6 小时前
【Linux】进程间关系与守护进程
linux·运维·服务器·计算机网络
S hh6 小时前
【Linux】进程地址空间
java·linux·运维·服务器·学习
苹果醋37 小时前
大模型实战--FastChat一行代码实现部署和各个组件详解
java·运维·spring boot·mysql·nginx
梁诚斌7 小时前
VSOMEIP代码阅读整理(1) - 网卡状态监听
运维·服务器·网络