nginx配置分发tomcat服务

点击上面  免费订阅本账号!

本公众号主要推送javaweb开发相关技术,基础知识点,同时会深入剖析复杂的问题,分享一些优秀的框架,大型项目经验,当今最流行的Javaweb技术,热点科技新闻,招聘信息,生活乐趣等等。点击上方的蓝字,这样您每天可以看到更多的java知识和资讯!完全是免费订阅,请放心关注。

搭建环境:

  1. ubuntu 16.04 LTS

  2. apache tomcat 7

  3. Java 7

  4. nginx/1.10.0 (ubuntu)

搭建过程:

注:本人在这里介绍自己安装的两种方式,一种使用官方源码包进行安装,另外一种使用ubuntu软件源进行安装,但推荐大家使用源码包进行安装,源码安装更易后期配置。

nginx官方中文文档地址

nginx gitbook官方文档地址

使用官方源码包进行安装

  1. 资源准备

  • pcre 源码包 (为了rewrite)

  • zlib 源码包 (为了压缩gzip)

  • ssl(openssl)源码包 (如果已安装可直接使用)

  • nginx源码包

  • gcc等系统编译工具(请自行准备,一般Linux系统都会自带)

  • 开始安装

    1. 分别解压三个压缩包

      tar -zxvf pcre.tar.gz

      tar -zxvf nginx.tar.gz

      tar -xvf zlib.tar.gz(注:官方源码包因为不是gzip压缩,所以不能使用 -z)

    2. 使用第一步解压的源码包路径,cd /usr/local/src/nginx-1.10.2目录,在这之前我已使用.configure,make,make install进行安装之前的pcre和zlib,输入以下指令,指定安装路径到/usr/local/nginx(路径可以自定义)

      sudo ./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.38 --with-zlib=/usr/local/src/zlib-1.2.8 --with-openssl=/usr/local/src/openssl-1.1.0c

      make

      make install

    3. 修改nginx.config

      vim /usr/local/nginx/nginx.conf

      在http节点的server{}标签内部末尾添加:

      “` location ^~ /service/ { #对url中包含/service/进行拦截分发

           proxy_pass   http://127.0.0.1:8080/; #代理服务地址
      
              proxy_redirect  off;
      
              proxy_set_header  X-Real-IP $remote_addr;
      
              proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;

      }“`

    4. 启动,停止服务 
      sudo /usr/local/nginx/nginx

      sudo /usr/local/nginx/nginx -s stop

    5. 启动tomcat项目 
      最后访问ip+/service/即可通过nginx访问到你的tomcat服务

    使用ubuntu软件源进行安装

    1. sudo apt install nginx

    2. vim /etc/nginx/sites-enabled/default

      在server{}标签内末尾出增加:

      “` location ^~ /service/ {#对url中包含/service/进行拦截分发

          proxy_pass   http://127.0.0.1:8080/;#代理服务地址
      
          proxy_redirect  off;
      
          proxy_set_header  X-Real-IP $remote_addr;
      
          proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      
      }```

      default完整配置如下:

      “` ## # You should look at the following URL’s in order to grasp a solid understanding

      # of Nginx configuration files in order to fully unleash the power of Nginx.
      
      # http://wiki.nginx.org/Pitfalls
      
      # http://wiki.nginx.org/QuickStart
      
      # http://wiki.nginx.org/Configuration
      
      #
      
      # Generally, you will want to move this file somewhere, and start with a clean
      
      # file but keep this around for reference. Or just disable in sites-enabled.
      
      #
      
      # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
      
      ##
      
      
      
      # Default server configuration
      
      #
      
      server {
      
          listen 80 default_server;
      
          listen [::]:80 default_server;
      
      
      
          # SSL configuration
      
          #
      
          # listen 443 ssl default_server;
      
          # listen [::]:443 ssl default_server;
      
          #
      
          # Note: You should disable gzip for SSL traffic.
      
          # See: https://bugs.debian.org/773332
      
          #
      
          # Read up on ssl_ciphers to ensure a secure configuration.
      
          # See: https://bugs.debian.org/765782
      
          #
      
          # Self signed certs generated by the ssl-cert package
      
          # Don't use them in a production server!
      
          #
      
          # include snippets/snakeoil.conf;
      
      
      
          root /var/www/html;
      
      
      
          # Add index.php to the list if you are using PHP
      
          index index.html index.htm index.nginx-debian.html;
      
      
      
          server_name _;
      
      
      
          location / {
      
              # First attempt to serve request as file, then
      
              # as directory, then fall back to displaying a 404.
      
              try_files $uri $uri/ =404;
      
          }
      
      
      
          # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      
          #
      
          #location ~ \.php$ {
      
          #   include snippets/fastcgi-php.conf;
      
          #
      
          #   # With php7.0-cgi alone:
      
          #   fastcgi_pass 127.0.0.1:9000;
      
          #   # With php7.0-fpm:
      
          #   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
      
          #}
      
      
      
          # deny access to .htaccess files, if Apache's document root
      
          # concurs with nginx's one
      
          #
      
          #location ~ /\.ht {
      
          #   deny all;
      
          #}
      
          location ^~ /service/ {
      
                  proxy_pass   http://127.0.0.1:8080/;
      
                  proxy_redirect  off;
      
                  proxy_set_header  X-Real-IP $remote_addr;
      
                  proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      
              }
      
      }
      
      
      
      
      
      # Virtual Host configuration for example.com
      
      #
      
      # You can move that to a different file under sites-available/ and symlink that
      
      # to sites-enabled/ to enable it.
      
      #
      
      #server {
      
      #   listen 80;
      
      #   listen [::]:80;
      
      #
      
      #   server_name example.com;
      
      #
      
      #   root /var/www/example.com;
      
      #   index index.html;
      
      #
      
      #   location / {
      
      #       try_files $uri $uri/ =404;
      
      #   }
      
      #}```
    3. service nginx restart

      最后访问ip+/service/即可通过nginx访问到你的tomcat服务,关于更多动静分离配置以及负载均衡,可参阅如上提供的官方文档,此博客未完待续,后期不定时更新。

      注:如果操作期间出现错误 可使用

      tail 或 cat /var/log/nginx/error.log

      查看错误日志再进行处理

    点击阅读全文阅读"程序员把女朋友聊没是怎样炼成的”

    有人用微信聊天,有人却在微信中学习,成长。下面是2016最HOT IT公众号,赶快试试新的关注方法吧!
    
    
    关注方式
    ★长按二维码,选择“识别图中二维码”进行关注。
    
    

     没看够?更多好文在阅读原文

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值