3.Nginx反向代理

1.单台代理

目的

        1.在浏览器访问一个地址:www.cpf.com。
        2.Nginx接受上面的请求。
        3.转发请求到tomcat。
        4.tomcat响应一个页面,页面中有:"tomcat hello !!!"。

步骤

1.安装nginx,并启动。

[root@localhost sbin]# ./nginx -s quit
[root@localhost sbin]# ps -ef | grep nginx
root 1558 1203 0 07:36 pts/1 00:00:00 grep --color=auto nginx
[root@localhost sbin]# pwd
/usr/sbin
[root@localhost sbin]# ./nginx
[root@localhost sbin]# ps -ef | grep nginx
root 1560 1 0 07:36 ? 00:00:00 nginx: master process ./nginx
nginx 1561 1560 0 07:36 ? 00:00:00 nginx: worker process
root 1563 1203 0 07:36 pts/1 00:00:00 grep --color=auto nginx
[root@localhost sbin]# ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
 inet 10.0.2.16 netmask 255.255.255.0 broadcast 10.0.2.255
 inet6 fe80::f112:dfa5:cc29:debb prefixlen 64 scopeid 0x20<link>
 ether 08:00:27:b7:f7:c4 txqueuelen 1000 (Ethernet)
 RX packets 667 bytes 58958 (57.5 KiB)
 RX errors 0 dropped 0 overruns 0 frame 0
 TX packets 590 bytes 123122 (120.2 KiB)
 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
 inet 127.0.0.1 netmask 255.0.0.0
 inet6 ::1 prefixlen 128 scopeid 0x10<host>
 loop txqueuelen 1 (Local Loopback)
 RX packets 68 bytes 5896 (5.7 KiB)
 RX errors 0 dropped 0 overruns 0 frame 0
 TX packets 68 bytes 5896 (5.7 KiB)
 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

浏览器访问:http://localhost:1080/。得到:
Welcome to nginx!
2.准备一个tomcat。
查看服务器上是否有tomcat?

[root@localhost sbin]# systemctl status tomcat
Unit tomcat.service could not be found.

结论:没有tomcat。
安装tomcat。

[root@localhost sbin]# yum install -y tomcat

查看服务器上tomcat

[root@localhost sbin]# systemctl status tomcat
?.tomcat.service - Apache Tomcat Web Application Container
 Loaded: loaded (/usr/lib/systemd/system/tomcat.service; disabled; vendor preset: disabled)
 Active: inactive (dead)

查看tomcat安装目录:

[root@localhost sbin]# cd /usr/share/tomcat/
[root@localhost tomcat]# ll
total 0
drwxr-xr-x. 2 root root 76 Apr 7 07:40 bin
lrwxrwxrwx. 1 root tomcat 11 Apr 7 07:40 conf -> /etc/tomcat
lrwxrwxrwx. 1 root tomcat 22 Apr 7 07:40 lib -> /usr/share/java/tomcat
lrwxrwxrwx. 1 root tomcat 15 Apr 7 07:40 logs -> /var/log/tomcat
lrwxrwxrwx. 1 root tomcat 22 Apr 7 07:40 temp -> /var/cache/tomcat/temp
lrwxrwxrwx. 1 root tomcat 23 Apr 7 07:40 webapps -> /var/lib/tomcat/webapps
lrwxrwxrwx. 1 root tomcat 22 Apr 7 07:40 work -> /var/cache/tomcat/work

创建页面:

[root@localhost tomcat]# cd webapps/
[root@localhost webapps]# ll
total 0
[root@localhost webapps]# mkdir ROOT
[root@localhost webapps]# ll
total 0
drwxr-xr-x. 2 root root 6 Apr 7 07:41 ROOT
[root@localhost webapps]#
[root@localhost ROOT]# vi index.html
[root@localhost ROOT]# cat index.html
tomcat hello !!!

启动tomcat
# 查看tomcat状态

[root@localhost ROOT]# systemctl status tomcat
?.tomcat.service - Apache Tomcat Web Application Container
 Loaded: loaded (/usr/lib/systemd/system/tomcat.service; disabled; vendor preset: disabled)
 Active: inactive (dead)

# 启动tomcat

[root@localhost ROOT]# systemctl start tomcat

# 查看tomcat状态

[root@localhost ROOT]# systemctl status tomcat
?.tomcat.service - Apache Tomcat Web Application Container
 Loaded: loaded (/usr/lib/systemd/system/tomcat.service; disabled; vendor preset: disabled)
 Active: active (running) since Thu 2022-04-07 07:44:02 EDT; 1s ago
 Main PID: 1755 (java)
 CGroup: /system.slice/tomcat.service
 ?..1755 /usr/lib/jvm/jre/bin/java -Djavax.sql.DataSource.Factory=org.apache.commons.dbcp.BasicDataSourceFactory -classpath /usr/share/tomcat/bin/bootstrap.jar:/usr/share/tomcat/bin/tomcat-juli.jar:/usr/...
Apr 07 07:44:04 localhost.localdomain server[1755]: Apr 07, 2022 7:44:04 AM org.apache.catalina.startup.VersionLoggerListener log
Apr 07 07:44:04 localhost.localdomain server[1755]: INFO: Command line argument: -Djava.endorsed.dirs=
Apr 07 07:44:04 localhost.localdomain server[1755]: Apr 07, 2022 7:44:04 AM org.apache.catalina.startup.VersionLoggerListener log
Apr 07 07:44:04 localhost.localdomain server[1755]: INFO: Command line argument: -Djava.io.tmpdir=/var/cache/tomcat/temp
Apr 07 07:44:04 localhost.localdomain server[1755]: Apr 07, 2022 7:44:04 AM org.apache.catalina.startup.VersionLoggerListener log
Apr 07 07:44:04 localhost.localdomain server[1755]: INFO: Command line argument: -Djava.util.logging.config.file=/usr/share/tomcat/conf/logging.properties
Apr 07 07:44:04 localhost.localdomain server[1755]: Apr 07, 2022 7:44:04 AM org.apache.catalina.startup.VersionLoggerListener log
Apr 07 07:44:04 localhost.localdomain server[1755]: INFO: Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
Apr 07 07:44:04 localhost.localdomain server[1755]: Apr 07, 2022 7:44:04 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
Apr 07 07:44:04 localhost.localdomain server[1755]: INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.pat...:/lib:/usr/lib
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ROOT]#

在tomcat的服务器上测试:

[root@localhost logs]# curl localhost:8080/index.html
tomcat hello !!!

在 宿主机 浏览器测试:访问:http://localhost:2080/
结果:tomcat hello !!!
重点来了:
1。在浏览器访问一个地址:www.cpf.com。
2。Nginx接受上面的请求。
3。转发请求到tomcat。
4。tomcat响应一个页面,页面中有:"tomcat hello !!!"。
通过nat网络分发:将本机2080转到虚拟机上的 nginx(80)。
转发:修改nginx配置文件: 

14 http {
 15 include /etc/nginx/mime.types;
 16 default_type application/octet-stream;
 17
 18 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
 19 '$status $body_bytes_sent "$http_referer" '
 20 '"$http_user_agent" "$http_x_forwarded_for"';
 21
 22 access_log /var/log/nginx/access.log main;
 23
 24 sendfile on;
 25 #tcp_nopush on;
 26
 27 keepalive_timeout 65;
 28
 29 #gzip on;
 30
 31 include /etc/nginx/conf.d/*.conf;
 32
 33 server{
 34 listen 80;
 35 server_name www.cpf.com;
 36 location / {
 37 proxy_pass http://localhost:8080;
 38 }
 39 }
 40 }

重新加载配置文件:

[root@localhost sbin]# ./nginx -s reload

2.多台代理

目的

1。浏览器访问:(http://www.cpf.com/beijing),通过nginx,跳转到一个tomcat上
(http://localhost:8081),在浏览器上显示:beijing。
2。浏览器访问:(http://www.cpf.com/shanghai),通过nginx,跳转到一个tomcat上
(http://localhost:8082),在浏览器上显示:shanghai。
准备两个tomcat:
访问官网:https://tomcat.apache.org/
在服务器上,根目录下,创建目录:

[root@localhost /]# mkdir tomcat

安装上传工具:

[root@localhost tomcat]# yum install lrzsz

上传tomcat文件

[root@localhost tomcat]# rz

创建两个tomcat目录:

[root@localhost tomcat]# mkdir tomcat8081
[root@localhost tomcat]# mkdir tomcat8082
[root@localhost tomcat]# ll
total 10332
-rw-r--r--. 1 root root 10577344 Apr 7 04:04 apache-tomcat-8.5.78.tar.gz
drwxr-xr-x. 2 root root 6 Apr 7 08:17 tomcat8081
drwxr-xr-x. 2 root root 6 Apr 7 08:17 tomcat8082

解压tomcat到上面两个目录中:tomcat8081和tomcat8082。分别需要解压。

[root@localhost tomcat8081]# tar -xvf ../apache-tomcat-8.5.78.tar.gz

停止原来的tomcat

[root@localhost tomcat8082]# systemctl stop tomcat

分别修改每个tomcat(tomcat8081和tomcat8082) 的配置文件:
修改点:

<Server port="8025" shutdown="SHUTDOWN">

修改点:

 <Connector port="8082" protocol="HTTP/1.1"
 connectionTimeout="20000"
 redirectPort="8443" />

分别启动两个tomcat(tomcat8081和tomcat8082)

[root@localhost bin]# pwd
/tomcat/tomcat8082/apache-tomcat-8.5.78/bin
[root@localhost bin]# ./startup.sh

分别在两个tomcat下,的 index.html 中 修改 beijing和shanghai,

[root@localhost ROOT]# cd /tomcat/tomcat8081/apache-tomcat-8.5.78/webapps/ROOT/
[root@localhost ROOT]# vi index.html
[root@localhost ROOT]# curl localhost:8081
beijing
[root@localhost ROOT]# curl localhost:8082
shanghai
[root@localhost ROOT]# cat index.html
beijing

3.Nginx配置

修改nginx.conf配置: 

server {
 listen 9001;
 server_name www.cpf.com;
 location ~ /beijing/ {
 proxy_pass http://localhost:8081
 }
 location ~ /shanghai/ {
 proxy_pass http://localhost:8082
 }
 }

目的:
1。浏览器访问:(http://www.cpf.com/beijing),通过nginx,跳转到一个tomcat上
(http://localhost:8081),在浏览器上显示:beijing。
2。浏览器访问:(http://www.cpf.com/shanghai),通过nginx,跳转到一个tomcat上
(http://localhost:8082),在浏览器上显示:shanghai。
修改一下2个tomcat:
北京:

[root@localhost webapps]# cp ROOT/index.html beijing/
[root@localhost webapps]# ll
total 4
drwxr-xr-x. 2 root root 24 Apr 7 08:56 beijing
drwxr-x---. 15 root root 4096 Apr 7 08:17 docs
drwxr-x---. 7 root root 99 Apr 7 08:17 examples
drwxr-x---. 6 root root 79 Apr 7 08:17 host-manager
drwxr-x---. 6 root root 114 Apr 7 08:17 manager
drwxr-x---. 3 root root 241 Apr 7 08:25 ROOT
[root@localhost webapps]# cd beijing/
[root@localhost beijing]# ll
total 4
-rw-r--r--. 1 root root 8 Apr 7 08:56 index.html
[root@localhost beijing]# curl localhost:8081/beijing
[root@localhost beijing]# curl localhost:8081/beijing/index.html
beijing

上海:

[root@localhost webapps]# mkdir shanghai
[root@localhost webapps]# cp ROOT/index.html shanghai/
[root@localhost webapps]# cd shanghai/
[root@localhost shanghai]# ll
total 4
-rw-r--r--. 1 root root 9 Apr 7 08:57 index.html
[root@localhost shanghai]# cat index.html
shanghai
[root@localhost shanghai]# curl localhost:8082/shanghai/index.html
shanghai

配置好网络映射(NAT)
http://www.cpf.com/beijing/index.html
http://www.cpf.com/shanghai/index.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值