nginx配置分发tomcat服务

本文档介绍了在ubuntu 16.04 LTS环境下,如何配置nginx以分发tomcat服务。内容包括使用官方源码包和ubuntu软件源两种安装nginx的方法,并详细说明了nginx配置文件的修改,特别是针对URL '/service/'的拦截和分发规则。完成配置后,可以通过访问IP+/service/来访问tomcat服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

搭建环境:

  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系统都会自带)

  2. 开始安装

    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

    查看错误日志再进行处理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值