Centos7下Nginx+Tomcat配置反向代理

一、反向代理简介

      反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。

二、

1、Nginx和Tomcat环境信息

Nginx:
ip:192.168.182.130   端口:80     安装位置:/usr/local/nginx

Tomcat1:
ip:192.168.182.130   端口:8080   安装位置:/usr/local/tomcat/apache-tomcat-8.0.50/
Tomcat2: 
ip:
192.168.182.131 端口:8080 安装位置:/usr/local/tomcat/apache-tomcat-8.0.50/

2、为了便于测试,修改两个tomcat站点ROOT的index.jsp页面为

<%--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--%>
<%@ page  pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy");
request.setAttribute("year", sdf.format(new java.util.Date()));
request.setAttribute("tomcatUrl", "http://tomcat.apache.org/");
request.setAttribute("tomcatDocUrl", "/docs/");
request.setAttribute("tomcatExamplesUrl", "/examples/");
%>
<!DOCTYPE html>
<html lang="en">
<h1>tomcat1</h1>
SessionId:<%=session.getId()%><br/>
SessionIP:<%=request.getServerName()%><br/>
</html>
View Code

3、启动两个tomcat,并测试tomcat能够正常访问

4、修改nginx配置信息,/usr/local/nginx/conf/nginx.conf,内容如下(需要将对应ip换成你自己的):

#user  nobody;
worker_processes  1;

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

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    upstream backend{
        server  192.168.182.131:8080;
        server  192.168.182.130:8080;
    }

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

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://backend;
            root   html;
            index  index.html index.htm;
        }

        location /stat {
            stub_status on;
        }
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ .php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}
View Code

其实只要修改两个地方就可以了(确保nginx安装对应模块),一个是新增

    upstream backend{
        server  192.168.182.131:8080;
        server  192.168.182.130:8080;
    }

另一个是在location / {} 里添加(这里的backend就是upstream所定义的):

proxy_pass http://backend;

5、启动nginx

6、测试

     浏览器访问192.168.182.130,ctrl + F5刷新浏览器,即可。

7、tomcat和nginx安装可以参考个人的随笔

     Linux环境下安装Tomcat

    CentOS7编译安装Nginx

yexiangyang 

moyyexy@gmail.com


 

原文地址:https://www.cnblogs.com/moy25/p/8496365.html