nginx反向代理多个服务器

在配置nginx时,发现了许多要注意的地方,在此记录一些

Nginx在线配置

root或者alias的区别

nginx是通过alias设置虚拟目录,在nginx的配置中,alias目录和root目录是有区别的:
1)alias指定的目录是准确的,即location匹配访问的path目录下的文件直接是在alias目录下查找的;
2)root指定的目录是location匹配访问的path目录的上一级目录,这个path目录一定要是真实存在root指定目录下的;
3)使用alias标签的目录块中不能使用rewrite的break(具体原因不明);另外,alias指定的目录后面必须要加上"/“符号!!
4)alias虚拟目录配置中,location匹配的path目录如果后面不带”/",那么访问的url地址中这个path目录后面加不加"/“不影响访问,访问时它会自动加上”/";
但是如果location匹配的path目录后面加上"/",那么访问的url地址中这个path目录必须要加上"/",访问时它不会自动加上"/"。如果不加上"/",访问就会失败!
5)root目录配置中,location匹配的path目录后面带不带"/",都不会影响访问。

proxy

proxy和location的规则进行匹配,可以监听一个端口,然后部署多个服务器
具体请参考:
Nginx——location常见配置指令,alias、root、proxy_pass

正确示范:

server {
	listen 80;
	listen [::]:80;

	server_name xxx.xx.xxx.xx; #公网ip

	# security
	include nginxconfig.io/security.conf;

	# reverse proxy
	#location /{
		#proxy_pass http://127.0.0.1:3008;
		#include nginxconfig.io/proxy.conf;
	#}
	
	location /admin/ {
		proxy_pass http://127.0.0.1:3008/;
		include nginxconfig.io/proxy.conf;
	}
	
	location /iview/ {
		proxy_pass http://127.0.0.1:3008/;
		include nginxconfig.io/proxy.conf;
	}

	# additional config
	include nginxconfig.io/general.conf;
}
http://xxx.xx.xxx.xx/admin
被代理为:
http://xxx.xx.xxx.xx/
http://xxx.xx.xxx.xx/admin/iview/
被代理为:
http://xxx.xx.xxx.xx/iview/

参考:
Nginx 的root和 alias
nginx 之 proxy_pass详解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Nginx反向代理多个服务器的实现方式是通过配置文件中的server块来实现的。每个server块对应一个域名或者IP地址,并定义了该域名或IP地址下的请求转发规则。\[1\] 例如,可以使用以下配置来实现反向代理多个服务器: ``` server { listen 80; server_name example.com; location / { proxy_pass http://backend_server1; } } server { listen 80; server_name example.org; location / { proxy_pass http://backend_server2; } } ``` 在上述配置中,当请求example.com时,Nginx会将请求转发到backend_server1;当请求example.org时,Nginx会将请求转发到backend_server2。\[2\] 需要注意的是,可以在location块中使用正则表达式来匹配特定的URL路径,并根据匹配结果进行转发。如果在server_name中使用了正则表达式,而location字段又使用了正则匹配,那么无法使用$1、$2等引用。解决方法是通过set指令将匹配结果赋值给一个命名的变量,然后在后续的配置中使用该变量。\[3\] 总结起来,Nginx反向代理多个服务器的实现方式是通过配置文件中的server块来定义不同域名或IP地址下的请求转发规则。可以使用正则表达式来匹配特定的URL路径,并通过set指令将匹配结果赋值给一个变量,然后在后续的配置中使用该变量。 #### 引用[.reference_title] - *1* [Nginx——反向代理多个服务器](https://blog.csdn.net/HeZhiYing_/article/details/104605493)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Nginx多域名多Server反向代理配置](https://blog.csdn.net/wgluser/article/details/8017312)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值