Nginx配置

反向代理配置

反向代理实例一

实现效果:使用nginx反向代理,访问www.123.com直接跳转到127.0.0.1:8080。

启动tomcat

cd apache-tomcat-10.0.6/
bin/startup.sh
firewall-cmd --add-port=8080/tcp --permanent
firewall-cmd --reload

浏览器访问出现tomcat页面
在这里插入图片描述

修改/etc/hosts文件,映射域名

[root@localhost etc]# vim hosts 
[root@localhost etc]# cat hosts 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 www.123.com
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@localhost etc]# 
  1. 访问 http://www.123.com:8080/ 出现tomcat页面
    在这里插入图片描述
  2. 配置完成之后,我们便可以通过www.123.com:8080访问到tomcat初始界面。
    那么如何只输入 www.123.com 便可以跳转到tomcat页面呢?使用到Nginx的反向代理。

在nginx.conf中修改server块配置

  1. 在nginx.conf中server块中修改server_name和location的配置
    在这里插入图片描述

nginx的server虚拟主机监听80端口,访问域名为www.123.com,不加端口时默认为80端口,故访问www.123.com域名的时候会跳转到127.0.0.1:8080路径上。

在浏览器上输入www.123.com 结果如下:
在这里插入图片描述

反向代理实例二

实现效果:使用nginx方向代理,根据访问的路径跳转到不同端口的服务中。
nginx监听端口为9001:
访问http://127.0.0.1:9001/edu/ 直接跳转到127.0.0.1:8081
访问http://127.0.0.1:9001/vod/ 直接跳转到127.0.0.1:8082

启动两个tomcat,一个端口8081,一个8082端口,并准备好测试的页面

# 准备8081的tomcat
[root@localhost ROOT]# pwd
/opt/webapps/apache-tomcat-10.0.6-8081/webapps/ROOT
[root@localhost ROOT]# mkdir edu
 [root@localhost apache-tomcat-10.0.6-8081]# cd webapps/
[root@localhost webapps]# ls
docs  edu  examples  host-manager  manager  ROOT
[root@localhost webapps]# cd edu
[root@localhost edu]# ls
index.html
[root@localhost edu]# cat index.html 
<html>
	<head>
	</head>
	<body>
		<h1>
			edu,8081.
		</h1>
	</body>
</html>
[root@localhost edu]# 

# 准备8082的tomcat
[root@localhost apache-tomcat-10.0.6-8082]# cd webapps/
[root@localhost webapps]# mkdir vod
 [root@localhost webapps]# ls
docs  examples  host-manager  manager  ROOT  vod
[root@localhost webapps]# cd vod
[root@localhost vod]# ls
index.html
[root@localhost vod]# cat index.html 
<html>
	<head>

	</head>
	<body>
	<h1>
		vod,8082,
	</h1>
	</body>
</html>

[root@localhost ROOT]# 

# 启动8081和8082的tomcat,测试如下
[root@localhost vod]# curl http://localhost:8081/edu/
<html>
	<head>
	</head>
	<body>
		<h1>
			edu,8081.
		</h1>
	</body>
</html>

[root@localhost vod]# curl http://localhost:8082/vod/
<html>
	<head>

	</head>
	<body>
	<h1>
		vod,8082.
	</h1>
	</body>
</html>

修改Nginx的配置

location指令说明
该指令用于匹配URL。
语法如下:
location [= | ~ | ~* | ^~] uri {
}

  1. = :用于不包含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配成功,就停止继续向下搜索并立即处理该请求。
  2. ~ :用于包含正则表达式的 uri 前,表示uri包含正则表达式,并且区分大小写。
  3. ~* :用于包含正则表达式的 uri 前,表示uri包含正则表达式,并且不区分大小写。
  4. ^~ :用于不包含正则表达式的uri前,要求Nginx服务器找到标识uri和请求字符串匹配度最高的location后,立即使用此location处理请求,而不使用location块中的正则uri和请求字符串做匹配。
  5. 注意:如果uri包含正则表达式,则必须要用 ~ 或者 ~* 标识。
  • 在http块中添加server{}虚拟主机。
worker_processes  3;

events {
    worker_connections  1024;
}

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

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  www.123.com;

        location / {
            proxy_pass http://127.0.0.1:8080;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
   
    server {
       listen 9001;
       server_name  localhost;

       location  ~  /edu/ {
          # 如果包含"/edu/",则会把url中的"localhost:9001"替换成"localhost:8081"
          proxy_pass  http://localhost:8081;
        }

       location  ~ /vod/ {
        # 如果包含"/vod/",则会把url中的"localhost:9001"替换成"localhost:8082"
         proxy_pass   http://localhost:8082;
        }
    }

}
  • 重启nginx
[root@localhost conf]# /usr/local/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf -s reload
  • 防火墙添加9001端口供外部访问
[root@localhost conf]# firewall-cmd --add-port=9001/tcp  --permanent
success
[root@localhost conf]# firewall-cmd --reload
success
[root@localhost conf]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: cockpit dhcpv6-client http ssh
  ports: 27017/tcp 80/tcp 8121-8124/tcp 8848/tcp 8080/tcp 9001/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
[root@localhost conf]# 

测试反向代理

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

融极

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

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

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

打赏作者

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

抵扣说明:

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

余额充值