文章目录

  • 1.nginx中常用命令
  • 2. nginx的配置文件
  • 3. 实现nginx 的反向代理跳转到后端的tomcat
  • 3.1 安装后端tomcat服务器
  • 3.2 配置nginx反向代理服务器
  • 3.3 客户端进行测试
  • 4. 实现nginx 的反向代理不同路径跳转到不同端口
  • 4.1 tomcat服务器建立两个不同端口的服务
  • 4.2 配置反向代理
  • 4.3 客户端测试


1.nginx中常用命令

1)命令位置:

[root@nginx ~]# cd /usr/local/nginx/sbin/
[root@nginx sbin]# ls
nginx

2)查看nginx版本号:

[root@nginx sbin]# ./nginx -v
nginx version: nginx/1.17.4

3)关闭nginx:

[root@nginx sbin]# ./nginx -s stop
[root@nginx sbin]# ps aux | grep nginx
root     17470  0.0  0.0 112648   960 pts/1    S+   14:18   0:00 grep --color=auto nginx

4)启动nginx:

[root@nginx sbin]# ./nginx 
[root@nginx sbin]# ps aux | grep nginx
root     17480  0.0  0.0  20504   612 ?        Ss   14:19   0:00 nginx: master process ./nginx
nobody   17481  0.0  0.0  23044  1376 ?        S    14:19   0:00 nginx: worker process
root     17483  0.0  0.0 112648   960 pts/1    S+   14:19   0:00 grep --color=auto nginx

5)重加载:修改了配置文件的内容,让配置文件重新加载生效

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

2. nginx的配置文件

1)配置问家的位置:

[root@nginx conf]# vim /usr/local/nginx/conf/nginx.conf

2)全局块:从配置文件开始到 events 块之间的内容,主要会设置一些影响 nginx 服务器整体运行的配置指令,主要包括配置运行 Nginx 服务器的用户(组)、允许生成的 worker process 数,进程 PID 存放路径、日志存放路径和类型以及配置文件的引入等。

#user  nobody;
worker_processes  1; # nginx处理并发的数量

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

2)event块:

events {
    worker_connections  1024; # 支持最大的连接数
}

4)http块:

http {
    include       mime.types;
    default_type  application/octet-stream;
    server {# 这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,该技术的产生是为了节省互联网服务器硬件成本
        listen       80; # 监听80端口
        server_name  localhost; # 主句名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / { # 
            root   html;
            index  index.html index.htm;
        }

3. 实现nginx 的反向代理跳转到后端的tomcat

实现的效果:在浏览器输入nginx地址,调转到第二台系统的tomcat主页中:

实验准备:

  • 一台nginx反向代理服务器:172.25.5.10/24
  • 一台tomcat服务器:172.25.5.15/24
  • 一台测试机

3.1 安装后端tomcat服务器

1)解压tomcat:

[root@tomcat ~]# ls
anaconda-ks.cfg               initial-setup-ks.cfg  
apache-tomcat-7.0.105.tar.gz
[root@tomcat ~]# tar zxf apache-tomcat-7.0.105.tar.gz 
[root@tomcat ~]# ls
anaconda-ks.cfg        apache-tomcat-7.0.105.tar.gz  
apache-tomcat-7.0.105  initial-setup-ks.cfg

2)启动tomcat:

[root@tomcat ~]# java -version
openjdk version "1.8.0_102"
OpenJDK Runtime Environment (build 1.8.0_102-b14)
OpenJDK 64-Bit Server VM (build 25.102-b14, mixed mode)
[root@tomcat ~]# cd apache-tomcat-7.0.105/bin/
[root@tomcat bin]# ./startup.sh 
Using CATALINA_BASE:   /root/apache-tomcat-7.0.105
Using CATALINA_HOME:   /root/apache-tomcat-7.0.105
Using CATALINA_TMPDIR: /root/apache-tomcat-7.0.105/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /root/apache-tomcat-7.0.105/bin/bootstrap.jar:/root/apache-tomcat-7.0.105/bin/tomcat-juli.jar
Tomcat started.

2)查看服务进程已经启动:

[root@tomcat bin]# ps aux | grep tomcat
root      3997  2.0  7.4 2301628 75952 pts/1   Sl   14:49   0:02 /usr/bin/java -Djava.util.logging.config.file=/root/apache-tomcat-7.0.105/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /root/apache-tomcat-7.0.105/bin/bootstrap.jar:/root/apache-tomcat-7.0.105/bin/tomcat-juli.jar -Dcatalina.base=/root/apache-tomcat-7.0.105 -Dcatalina.home=/root/apache-tomcat-7.0.105 -Djava.io.tmpdir=/root/apache-tomcat-7.0.105/temp org.apache.catalina.startup.Bootstrap start
root      4080  3.1  6.0 2288264 61776 pts/1   Sl   14:50   0:01 /usr/bin/java -Djava.util.logging.config.file=/root/apache-tomcat-7.0.105/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /root/apache-tomcat-7.0.105/bin/bootstrap.jar:/root/apache-tomcat-7.0.105/bin/tomcat-juli.jar -Dcatalina.base=/root/apache-tomcat-7.0.105 -Dcatalina.home=/root/apache-tomcat-7.0.105 -Djava.io.tmpdir=/root/apache-tomcat-7.0.105/temp org.apache.catalina.startup.Bootstrap start

3)对外开放访问端口8080:

[root@tomcat ~]# firewall-cmd --permanent --add-port=8080/tcp
success
[root@tomcat ~]# firewall-cmd --reload
success
[root@tomcat ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens3
  sources: 
  services: dhcpv6-client http ssh
  ports: 8080/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules:

4)客户端测试一下(访问成功):

linux 查看 nginx版本 linux nginx版本查看命令_linux 查看 nginx版本

3.2 配置nginx反向代理服务器

1)修改配置文件:写入转发

[root@nginx ~]# cd /usr/local/nginx/conf/
[root@nginx conf]# ls
fastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.default   uwsgi_params
fastcgi.conf.default  koi-utf                 mime.types.default  scgi_params          uwsgi_params.default
fastcgi_params        koi-win                 nginx.conf          scgi_params.default  win-utf
[root@nginx conf]# vim nginx.conf

server {
        listen       80;
        server_name  172.25.5.10; # 访问这个网卡

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html; 
            proxy_pass http://172.25.5.15:8080; # 就转发到tomcat服务器
            index  index.html index.htm;
        }

2)重新加载:

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

3.3 客户端进行测试

1)客户端写入代理服务器的域名解析:

[root@client ~]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

172.25.5.10 www.song.com

2)浏览器访问域名:反向代理成功

linux 查看 nginx版本 linux nginx版本查看命令_centos_02

4. 实现nginx 的反向代理不同路径跳转到不同端口

4.1 tomcat服务器建立两个不同端口的服务

1)建立服务目录:

[root@tomcat ~]# mkdir tomcat8080
[root@tomcat ~]# mkdir tomcat8081

2)复制安装包:

[root@tomcat ~]# cp apache-tomcat-7.0.105.tar.gz tomcat8080
[root@tomcat ~]# cp apache-tomcat-7.0.105.tar.gz tomcat8081

3)启动8080端口的tomcat:

[root@tomcat ~]# cd tomcat8080/
[root@tomcat tomcat8080]# ls
apache-tomcat-7.0.105.tar.gz
[root@tomcat tomcat8080]# tar zxf apache-tomcat-7.0.105.tar.gz 
[root@tomcat tomcat8080]# cd apache-tomcat-7.0.105/
[root@tomcat apache-tomcat-7.0.105]# cd bin/
[root@tomcat bin]# ./startup.sh 
Using CATALINA_BASE:   /root/tomcat8080/apache-tomcat-7.0.105
Using CATALINA_HOME:   /root/tomcat8080/apache-tomcat-7.0.105
Using CATALINA_TMPDIR: /root/tomcat8080/apache-tomcat-7.0.105/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /root/tomcat8080/apache-tomcat-7.0.105/bin/bootstrap.jar:/root/tomcat8080/apache-tomcat-7.0.105/bin/tomcat-juli.jar
Tomcat started.

4)启动8081端口的tomcat:

[root@tomcat ~]# cd tomcat8081/
[root@tomcat tomcat8081]# ls
apache-tomcat-7.0.105.tar.gz
[root@tomcat tomcat8081]# tar zxf apache-tomcat-7.0.105.tar.gz 
[root@tomcat tomcat8081]# cd apache-tomcat-7.0.105/
[root@tomcat apache-tomcat-7.0.105]# cd conf/
[root@tomcat conf]# vim server.xml 

<Server port="8015" shutdown="SHUTDOWN">
<Connector port="8081" protocol="HTTP/1.1"
 <Connector protocol="AJP/1.3"
               address="::1"
               port="8019"
               redirectPort="8443" />

[root@tomcat conf]# cd ..
[root@tomcat apache-tomcat-7.0.105]# cd bin/
[root@tomcat bin]# ./startup.sh 
Using CATALINA_BASE:   /root/tomcat8081/apache-tomcat-7.0.105
Using CATALINA_HOME:   /root/tomcat8081/apache-tomcat-7.0.105
Using CATALINA_TMPDIR: /root/tomcat8081/apache-tomcat-7.0.105/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /root/tomcat8081/apache-tomcat-7.0.105/bin/bootstrap.jar:/root/tomcat8081/apache-tomcat-7.0.105/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat bin]# firewall-cmd --permanent --add-port=8081/tcp
success
[root@tomcat bin]# firewall-cmd --reload
success
[root@tomcat bin]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens3
  sources: 
  services: dhcpv6-client http ssh
  ports: 8081/tcp 8080/tcp 22/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules:

5)客户端测试:

linux 查看 nginx版本 linux nginx版本查看命令_linux_03


linux 查看 nginx版本 linux nginx版本查看命令_linux_04


6)写入8080的发布页面:

[root@tomcat ~]# cd tomcat8080/apache-tomcat-7.0.105/webapps/
[root@tomcat webapps]# mkdir edu
[root@tomcat webapps]# cd edu/
[root@tomcat edu]# vim a.html
[root@tomcat edu]# cat a.html 
8080!!!

linux 查看 nginx版本 linux nginx版本查看命令_tomcat_05

7)写入8081的发布页面:

[root@tomcat ~]# cd tomcat8081/apache-tomcat-7.0.105/webapps/
[root@tomcat webapps]# mkdir vod
[root@tomcat webapps]# cd vod/
[root@tomcat vod]# vim a.html
[root@tomcat vod]# cat a.html 
8081!!!

linux 查看 nginx版本 linux nginx版本查看命令_tomcat_06

4.2 配置反向代理

1)

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf

server {
        listen       9001;
        server_name  172.25.5.10;

        location ~ /edu/ {
            root   html;
            proxy_pass http://172.25.5.15:8080
            index  a.html index.html index.htm;
        }
        
        location ~ /vod/ {
            root   html;
            proxy_pass http://172.25.5.15:8081
            index  a.html index.html index.htm;
        }
    }

2)开放9001端口:

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
[root@nginx ~]# firewall-cmd --permanent --add-port=9001/tcp
success
[root@nginx ~]# firewall-cmd --reload
success
[root@nginx ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens3
  sources: 
  services: dhcpv6-client http ssh
  ports: 80/tcp 9001/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules:

3)重启nginx:

[root@nginx ~]# cd /usr/local/nginx/sbin/
[root@nginx sbin]# ./nginx -s reload

4.3 客户端测试

1)访问代理服务器9001端口的edu:到8080端口

linux 查看 nginx版本 linux nginx版本查看命令_tomcat_07

2)访问代理服务器9001端口的vod:到8081端口

linux 查看 nginx版本 linux nginx版本查看命令_centos_08