詳解Ngigx+Tomcat配置動(dòng)靜分離,負(fù)載均衡
由于公司使用過Ngnix,對于剛接觸Nginx來說,感覺有些好奇,于是研究了下。
本人在windows下使用的版本是nginx-1.8.1:
1. 啟動(dòng)Ngnix
雙擊nginx-1.8.1文件夾中nginx.exe,當(dāng)任務(wù)管理器中存在兩個(gè)nginx進(jìn)程時(shí),則說明啟動(dòng)成功!
2. Ngnix常用命令
- nginx -s stop 強(qiáng)制關(guān)閉
- nginx -s quit 安全關(guān)閉
- nginx -s reload 改變配置文件的時(shí)候,重啟nginx工作進(jìn)程,來時(shí)配置文件生效
- nginx -s reopen 打開日志文件
3. Nginx配置
下面配置綜合了網(wǎng)上的資料,記下,防止自己忘記。
#Nginx所用用戶和組
#user nobody;
#工作的子進(jìn)程數(shù)量(通常等于CPU數(shù)量或者2倍于CPU)
worker_processes 1;
#錯(cuò)誤日志存放路徑
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#指定pid存放文件
#pid logs/nginx.pid;
events {
#使用網(wǎng)絡(luò)IO模型linux建議epoll,F(xiàn)reeBSD建議采用kqueue
#use epoll;
#使用epoll模型提高性能 win下不需要
#use epoll;
#允許最大連接數(shù)
worker_connections 1024;
}
http {
#擴(kuò)展名與文件類型映射表
include mime.types;
#默認(rèn)類型
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;
# 啟用內(nèi)核復(fù)制模式,應(yīng)該保持開啟達(dá)到最快IO效率
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
# HTTP1.1支持持久連接alive
# 降低每個(gè)連接的alive時(shí)間可在一定程度上提高可響應(yīng)連接數(shù)量,所以一般可適當(dāng)降低此值
keepalive_timeout 65;
# 啟動(dòng)gzip壓縮功能設(shè)置,有效降低網(wǎng)絡(luò)流量
gzip on;
gzip_min_length 1k; #最小1K
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascripttext/css application/xml;
gzip_vary on;
# 靜態(tài)文件緩存
# 最大緩存數(shù)量,文件未使用存活期
open_file_cache max=655350 inactive=20s;
# 驗(yàn)證緩存有效期時(shí)間間隔
open_file_cache_valid 30s;
# 有效期內(nèi)文件最少使用次數(shù)
open_file_cache_min_uses 2;
#xbq add
#upstream作負(fù)載均衡,在此配置需要輪詢的服務(wù)器地址和端口號(hào),max_fails為允許請求失敗的次數(shù),默認(rèn)為1.
#weight為輪詢權(quán)重,根據(jù)不同的權(quán)重分配可以用來平衡服務(wù)器的訪問率。
upstream hostname {
server 127.0.0.1:9000 max_fails=0 weight=2;
server 127.0.0.1:9001 max_fails=0 weight=2;
}
server {
listen 8181;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
root /img; #在nginx-1.8.1文件夾中新建img文件夾,用于存放靜態(tài)資源
location / {
#root html;
#index index.html index.htm;
#xbq add
proxy_pass http://hostname;
#下面三條指令允許重新定義和添加一些將被轉(zhuǎn)移到被代理服務(wù)器的請求頭部信息
# 請求頭中Host信息
proxy_set_header Host $host;
# 真實(shí)的客戶端IP
proxy_set_header X-Real-IP $remote_addr;
# 代理路由信息,此處取IP有安全隱患
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 真實(shí)的用戶訪問協(xié)議
proxy_set_header X-Forwarded-Proto $scheme;
# 默認(rèn)值default,
# 后端response 302時(shí) tomcat header中l(wèi)ocation的host是http://192.168.1.62:8080
# 因?yàn)閠omcat收到的請求是nginx發(fā)過去的, nginx發(fā)起的請求url host是http://192.168.1.62:8080
# 設(shè)置為default后,nginx自動(dòng)把響應(yīng)頭中l(wèi)ocation host部分替換成當(dāng)前用戶請求的host部分
# 網(wǎng)上很多教程將此值設(shè)置成 off,禁用了替換,
# 這樣用戶瀏覽器收到302后跳到http://192.168.1.62:8080,直接將后端服務(wù)器暴露給瀏覽器
# 所以除非特殊需要,不要設(shè)置這種畫蛇添足的配置
proxy_redirect default;
client_max_body_size 10m; #允許客戶端請求的最大單文件字節(jié)數(shù)
client_body_buffer_size 128k; #緩沖區(qū)代理緩沖用戶端請求的最大字節(jié)數(shù)
proxy_connect_timeout 90; #nginx跟后端服務(wù)器連接超時(shí)時(shí)間
proxy_read_timeout 90; #連接成功后,后端服務(wù)器響應(yīng)時(shí)間
proxy_buffer_size 4k; #設(shè)置代理服務(wù)器(nginx)保存用戶頭信息的緩沖區(qū)大小
proxy_buffers 6 32k; #proxy_buffers緩沖區(qū),網(wǎng)頁平均在32k以下的話,這樣設(shè)置
proxy_busy_buffers_size 64k;#高負(fù)荷下緩沖大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #設(shè)定緩存文件夾大小,大于這個(gè)值,將從upstream服務(wù)器傳
}
#xbq add
#配置Nginx動(dòng)靜分離,定義的靜態(tài)頁面直接從/usr/nginxStaticFile(Nginx發(fā)布目錄)讀取。
location ~\.(gif|jpg|jpeg|png|css|js|php)$ {
#expires定義用戶瀏覽器緩存的時(shí)間為7天,如果靜態(tài)頁面不常更新,可以設(shè)置更長,這樣可以節(jié)省帶寬和緩解服務(wù)器的壓力 E:/staticResource;
expires 7d;
}
#xbq add
#啟用nginx status 監(jiān)聽頁面
location /nginxstatus {
stub_status on;
access_log 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;
# }
#}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Red Hat Linux,Apache2.0+Weblogic9.2負(fù)載均衡集群安裝配置
- 用apache和tomcat搭建集群(負(fù)載均衡)
- tomcat6_apache2.2_ajp 負(fù)載均衡加集群實(shí)戰(zhàn)分享
- 簡單測試Apache是如何完成負(fù)載均衡策略配置
- Apache實(shí)現(xiàn)Web Server負(fù)載均衡詳解(不考慮Session版)
- Apache2.2以上版本與Tomcat整合配置及負(fù)載均衡實(shí)現(xiàn)
- Apache負(fù)載均衡設(shè)置方法 mod_proxy使用介紹
- apache負(fù)載均衡的安裝和實(shí)現(xiàn)方法
- Nginx+Tomcat高性能負(fù)載均衡集群搭建教程
- Nginx+Tomcat搭建高性能負(fù)載均衡集群的實(shí)現(xiàn)方法
- nginx+tomcat實(shí)現(xiàn)Windows系統(tǒng)下的負(fù)載均衡搭建教程
- 詳解基于Centos7+Nginx+Tomcat8的負(fù)載均衡服務(wù)器的搭建
- 詳解Nginx + Tomcat 反向代理 負(fù)載均衡 集群 部署指南
- nginx+tomcat實(shí)現(xiàn)負(fù)載均衡,使用redis session共享
- Nginx與Tomcat實(shí)現(xiàn)動(dòng)靜態(tài)分離和負(fù)載均衡
- Nginx+Tomcat+Https 服務(wù)器負(fù)載均衡配置實(shí)踐方案詳解
- linux下Nginx+Tomcat負(fù)載均衡配置方法
- windows下nginx+tomcat配置負(fù)載均衡的方法
- Debian下搭建Nginx和Tomcat服務(wù)器實(shí)現(xiàn)負(fù)載均衡的方案
- Windows下Apache+Tomcat7負(fù)載均衡配置方法詳解
相關(guān)文章
詳解Nginx配置SSL證書實(shí)現(xiàn)Https訪問
這篇文章主要介紹了詳解Nginx配置SSL證書實(shí)現(xiàn)Https訪問,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Ubuntu環(huán)境下安裝部署Nginx詳細(xì)步驟(有網(wǎng))
Nginx是一個(gè)開源的?HTTP?網(wǎng)絡(luò)服務(wù)器,下面這篇文章主要給大家介紹了關(guān)于Ubuntu環(huán)境下安裝部署Nginx(有網(wǎng))的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
Nginx配置Basic Auth登錄認(rèn)證的實(shí)現(xiàn)方法
這篇文章主要介紹了Nginx配置Basic Auth登錄認(rèn)證的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09
Nginx Proxy 代理測試的實(shí)現(xiàn)
本文主要介紹了Nginx Proxy 代理測試的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02

