nginx配置分发tomcat服务

www.allocmem.com · · 4346 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

##搭建环境:

  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

    查看错误日志再进行处理

本文来自:www.allocmem.com

感谢作者:www.allocmem.com

查看原文:nginx配置分发tomcat服务

4346 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传