Nginx反向代理
# Nginx反向代理
# 实现效果实例1
1、打开浏览器,在浏览器地址栏中输入地址www.123.com,跳转到linux的tomcat主页面。
# 准备工作
(1)安装Nginx在linux服务器
(2)安装Tomcat,使用默认端口8080
# 1、安装Tomcat
安装Java-1.8.0-jdk
[root@nginx ~]# yum install -y java
[root@nginx ~]# java -version
openjdk version "1.8.0_322"
OpenJDK Runtime Environment (build 1.8.0_322-b06)
OpenJDK 64-Bit Server VM (build 25.322-b06, mixed mode)
安装tomcat
[root@nginx ~]# tar zxvf apache-tomcat-8.5.75.tar.gz
[root@nginx ~]# cd apache-tomcat-8.5.75/
[root@nginx apache-tomcat-8.5.75]# cd bin/
[root@nginx bin]# ./startup.sh
Using CATALINA_BASE: /root/apache-tomcat-8.5.75
Using CATALINA_HOME: /root/apache-tomcat-8.5.75
Using CATALINA_TMPDIR: /root/apache-tomcat-8.5.75/temp
Using JRE_HOME: /usr
Using CLASSPATH: /root/apache-tomcat-8.5.75/bin/bootstrap.jar:/root/apache-tomcat-8.5.75/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
# 2、关闭防火墙
[root@nginx bin]# firewall-cmd --list-alnot running
[root@nginx bin]# firewall-cmd --add-service=http --permanent
[root@nginx bin]# firewall-cmd --add-port=80/tcp --permanent
[root@nginx bin]# firewall-cmd --add-port=8080/tcp --permanent
[root@nginx bin]# firewall-cmd --reload
# 3、修改Nginx配置文件
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name 192.168.0.135; # localhost配置IP地址
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
proxy_pass http://127.0.0.1:8080; # 添加反向代理地址
index index.html index.htm;
}
[root@nginx ~]# /usr/local/nginx/sbin/nginx -s reload
在本机配置hosts映射,然后浏览器访问
# 实现效果实例2
使用nginx反向代理,根据访问的路径跳转到不同的端口服务中,nginx监听端口为9001
访问http://127.0.0.1:9001/edu/ 直接跳转到127.0.0.1:8080
访问http://127.0.0.1:9001/vod/ 直接跳转到127.0.0.1:8082
# 准备工作
准备两台tomcat服务器,一台8080,一台8081
在两台tomcat里面webapps目录中,创建名称是edu的文件夹,在edu文件夹中创建页面a.html,用于测试
# 1、准备两个tomcat服务器
[root@nginx ~]# cd /usr/src/
[root@nginx src]# mkdir tomcat8080
[root@nginx src]# mkdir tomcat8081
[root@nginx src]# cp /root/apache-tomcat-8.5.75.tar.gz tomcat8080/
[root@nginx src]# cp /root/apache-tomcat-8.5.75.tar.gz tomcat8081/
# 2、启动服务、修改端口
[root@nginx src]# ./tomcat8080/apache-tomcat-8.5.75/bin/startup.sh
Using CATALINA_BASE: /usr/src/tomcat8080/apache-tomcat-8.5.75
Using CATALINA_HOME: /usr/src/tomcat8080/apache-tomcat-8.5.75
Using CATALINA_TMPDIR: /usr/src/tomcat8080/apache-tomcat-8.5.75/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/src/tomcat8080/apache-tomcat-8.5.75/bin/bootstrap.jar:/usr/src/tomcat8080/apache-tomcat-8.5.75/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
[root@nginx src]# cd tomcat8081/apache-tomcat-8.5.75/
[root@nginx apache-tomcat-8.5.75]# cd conf/
[root@nginx conf]# vim server.xml
<Server port="8015" shutdown="SHUTDOWN"> #8005修改成8015
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
·
·
·
<Connector port="8081" protocol="HTTP/1.1" #8080修改成8081
connectionTimeout="20000"
redirectPort="8443" />
·
·
·
<!-- Define an AJP 1.3 Connector on port 8009 -->
<!--
<Connector protocol="AJP/1.3"
address="::1"
port="8019" #8009修改成8019
redirectPort="8443" />
-->
[root@nginx conf]# cd ..
[root@nginx apache-tomcat-8.5.75]# cd bin/
[root@nginx bin]# ./startup.sh
Using CATALINA_BASE: /usr/src/tomcat8081/apache-tomcat-8.5.75
Using CATALINA_HOME: /usr/src/tomcat8081/apache-tomcat-8.5.75
Using CATALINA_TMPDIR: /usr/src/tomcat8081/apache-tomcat-8.5.75/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/src/tomcat8081/apache-tomcat-8.5.75/bin/bootstrap.jar:/usr/src/tomcat8081/apache-tomcat-8.5.75/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
# 3、创建文件夹、测试页面
[root@nginx src]# cd tomcat8080/apache-tomcat-8.5.75/webapps/
[root@nginx webapps]# mkdir edu
[root@nginx webapps]# echo "8080 tomcat" > edu/a.html
[root@nginx src]# cd tomcat8081/apache-tomcat-8.5.75/webapps/
[root@nginx webapps]# mkdir vod
[root@nginx webapps]# echo "8081 tomcat" > vod/a.html
# 4、配置Nginx配置文件
添加如下的nginx配置
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 9001;
server_name 192.168.0.135;
location ~ /edu/ {
proxy_pass http://127.0.0.1:8080;
}
location ~ /vod/ {
proxy_pass http://127.0.0.1:8081;
}
}
[root@nginx ~]# /usr/local/nginx/sbin/nginx -s reload
上次更新: 2023/11/28, 22:03:59