nginx配置分發tomcat服務
搭建環境:
ubuntu 16.04 LTS
apache tomcat 7
java 7
nginx/1.10.0 (ubuntu)
搭建過程:
注:本人在這裡介紹自己安裝的兩種方式,一種使用官方原始碼包進行安裝,另外一種使用ubuntu軟體源進行安裝,但推薦大家使用原始碼包進行安裝,原始碼安裝更易後期配置。
使用官方原始碼包進行安裝
資源準備
pcre 原始碼包 (為了rewrite)
zlib 原始碼包 (為了壓縮gzip)
ssl(openssl)原始碼包 (如果已安裝可直接使用)
nginx原始碼包
gcc等系統編譯工具(請自行準備,一般linux系統都會自帶)
開始安裝
分別解壓三個壓縮包
tar -zxvf pcre.tar.gz
tar -zxvf nginx.tar.gz
tar -xvf zlib.tar.gz(注:官方原始碼包因為不是gzip壓縮,所以不能使用 -z)
使用第一步解壓的原始碼包路徑,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
修改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;
}“`
啟動,停止服務
sudo /usr/local/nginx/nginx
sudo /usr/local/nginx/nginx -s stop
- 啟動tomcat專案
最後訪問ip+/service/即可通過nginx訪問到你的tomcat服務
使用ubuntu軟體源進行安裝
sudo apt install nginx
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; # } #}```
service nginx restart
最後訪問ip+/service/即可通過nginx訪問到你的tomcat服務,關於更多動靜分離配置以及負載均衡,可參閱如上提供的官方文件,此部落格未完待續,後期不定時更新。
注:如果操作期間出現錯誤 可使用
tail 或 cat /var/log/nginx/error.log
檢視錯誤日誌再進行處理