Nginx 反向代理搭建 (单机模拟)

模拟环境  : centos7     VM虚拟机   jdk1.8   tomcat 8.0  nginx 


一  , 下载安装 jdk   ,配置 java  环境

1 .下载linuxjdk
2 。将jdk 解压
Nginx 反向代理搭建 (单机模拟)
3.配置环境变量
[[email protected] download]# vi /etc/profile
在最后面加上        中间用的是:(冒号) 不是 ;(分号)
#set java environment
export JAVA_HOME=/root/jdk1.8.0_161
export CLASSPATH=.:$JAVA_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin           
 
4.使环境变量生效
source /etc/profile

Nginx 反向代理搭建 (单机模拟)


二  ,下载安装tomcat  ,搭建两个简单的web 项目

        1.下载 安装 tomcat

        2 .解压 tomcat 压缩包  (解压两份,对tomcat 重命名一下)
               tar -zxvf apache-tomcat-8.5.29.tar.gz

               mv apache-tomcat-8.5.29 ./apache-tomcat-8.5.29-8080

               tar -zxvf apache-tomcat-8.5.29.tar.gz

               mv apache-tomcat-8.5.29 ./apache-tomcat-8.5.29-9090

                Nginx 反向代理搭建 (单机模拟)

                由于8080 为tomcat 的默认端口  ,所以对8080 的tomcat  不做 server.xml 配置

                更改  9090 的server.xml  配置文件  (修改tomcat的端口信息)

                vim apache-tomcat-8.5.29-9090/conf/server.xml            

                Nginx 反向代理搭建 (单机模拟)

                Nginx 反向代理搭建 (单机模拟)

                Nginx 反向代理搭建 (单机模拟)

                Nginx 反向代理搭建 (单机模拟)

                Nginx 反向代理搭建 (单机模拟)

                Nginx 反向代理搭建 (单机模拟)

        3 。更改 tomcat ROOT 项目的 index.jsp 页面

                 vim apache-tomcat-8.5.29-8080/webapps/ROOT/index.jsp 

                在  页面的 body  下 加入   下面 便于我们观察   代理结果    

                     <div>

                        <h1> 当前服务器为  8080  服务器  </h1>
                    </div>

                Nginx 反向代理搭建 (单机模拟)

            同理   编辑  9090 下面的

                vim apache-tomcat-8.5.29-9090/webapps/ROOT/index.jsp

                Nginx 反向代理搭建 (单机模拟)


三  ,下载安装 nginx 

            1 。下载 nginx 

            wget http://nginx.org/download/nginx-1.10.1.tar.gz

            2 ,解压安装 :

            tar -zxvf nginx-1.10.1.tar.gz

            3, 安装  c 编译器  ,如果已经安装就不需要安装了

            yum install gcc gcc-c++

            4 ,开始编译 nginx 源码  进行安装

            cd nginx-1.10.1

            ./configure --prefix=/nginx      prefix  指定编译输出目录

            Nginx 反向代理搭建 (单机模拟)

            安装 PCRE 库

            wget http://nchc.dl.sourceforge.net/project/pcre/pcre/8.39/pcre-8.39.tar.gz

            tar -zxvf pcre-8.39.tar.gz

            cd pcre-8.39

            ./configure

            make && make install    

         安装zlib 库

        yum install -y zip unzip
        unzip zlib1211.zip
        cd zlib-1.2.11/
        ./configure
        make && make install
        
        安装 openssl  openssl:https协议所要使用的模块
        wget http://www.openssl.org/source/openssl-1.0.0e.tar.gz
        tar -zxvf openssl-1.0.0e.tar.gz 
        cd openssl-1.0.0e
        ./config
        make && make install
        
        cd nginx-1.10.1
        ./configure --prefix=/nginx    --with-pcre=/root/pcre-8.39
            Nginx 反向代理搭建 (单机模拟)
            make && make install
            Nginx 反向代理搭建 (单机模拟)

            5  。启动nginx
                    cd  /nginx/sbin
                    ./nginx   
            Nginx 反向代理搭建 (单机模拟)
                
            nginx  安装成功 

        

四  。配置 ngxin  反向代理

            修改 Windows 下的 host 文件 配置 域名解析

            C:\Windows\System32\drivers\etc         设置文件的修改权限

                     Nginx 反向代理搭建 (单机模拟)

  该ip  39.108.172.143   为虚拟机ip (相当于服务器ip)

   Nginx 反向代理搭建 (单机模拟)              修改 nginx  的配置文件

               cd /nginx/conf

               vim nginx.conf

        在 http { }  里面添加 如下配置

        upstream zegoto8080  {
        server 127.0.0.1:8080; #Apache
        }



        upstream zegoto9090  {
                server 127.0.0.1:9090; #Apache
        }


server {
listen       80;
server_name  zegoto8080;
access_log  logs/host.access.log  main;
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
# 配置反向代理 的web 服务

location / {
proxy_pass   http://zegoto8080;
 
#Proxy Settings
proxy_redirect     off;
proxy_set_header   Host             $host;
proxy_set_header   X-Real-IP        $remote_addr;
proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout      90;
proxy_send_timeout         90;
proxy_read_timeout         90;
proxy_buffer_size          4k;
proxy_buffers              4 32k;
proxy_busy_buffers_size    64k;
proxy_temp_file_write_size 64k;
}

#error_page  404              /404.html;

error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}
   
    }
    
server {
listen       80;
server_name  zegoto9090;
access_log  logs/host.access.log  main;
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
# 配置反向代理 的web 服务

location / {
proxy_pass   http://zegoto9090;
 
#Proxy Settings
proxy_redirect     off;
proxy_set_header   Host             $host;
proxy_set_header   X-Real-IP        $remote_addr;
proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout      90;
proxy_send_timeout         90;
proxy_read_timeout         90;
proxy_buffer_size          4k;
proxy_buffers              4 32k;
proxy_busy_buffers_size    64k;
proxy_temp_file_write_size 64k;
}

#error_page  404              /404.html;

error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}
   
    }

    

    保存配置  重新 启动 nginx   和 tomcat 服务器

        Nginx 反向代理搭建 (单机模拟)

        
        Nginx 反向代理搭建 (单机模拟)
            
       五  。配置完成  进行代理测试

        Nginx 反向代理搭建 (单机模拟)

        
Nginx 反向代理搭建 (单机模拟)

 


            

            


    http://zegoto.cn/  我的个人网站