詳解Ngigx+Tomcat配置動靜分離,負載均衡
由于公司使用過Ngnix,對于剛接觸Nginx來說,感覺有些好奇,于是研究了下。
本人在windows下使用的版本是nginx-1.8.1:
1. 啟動Ngnix
雙擊nginx-1.8.1文件夾中nginx.exe,當任務管理器中存在兩個nginx進程時,則說明啟動成功!
2. Ngnix常用命令
- nginx -s stop 強制關閉
- nginx -s quit 安全關閉
- nginx -s reload 改變配置文件的時候,重啟nginx工作進程,來時配置文件生效
- nginx -s reopen 打開日志文件
3. Nginx配置
下面配置綜合了網上的資料,記下,防止自己忘記。
#Nginx所用用戶和組 #user nobody; #工作的子進程數量(通常等于CPU數量或者2倍于CPU) worker_processes 1; #錯誤日志存放路徑 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #指定pid存放文件 #pid logs/nginx.pid; events { #使用網絡IO模型linux建議epoll,FreeBSD建議采用kqueue #use epoll; #使用epoll模型提高性能 win下不需要 #use epoll; #允許最大連接數 worker_connections 1024; } http { #擴展名與文件類型映射表 include mime.types; #默認類型 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; # 啟用內核復制模式,應該保持開啟達到最快IO效率 sendfile on; #tcp_nopush on; #keepalive_timeout 0; # HTTP1.1支持持久連接alive # 降低每個連接的alive時間可在一定程度上提高可響應連接數量,所以一般可適當降低此值 keepalive_timeout 65; # 啟動gzip壓縮功能設置,有效降低網絡流量 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)文件緩存 # 最大緩存數量,文件未使用存活期 open_file_cache max=655350 inactive=20s; # 驗證緩存有效期時間間隔 open_file_cache_valid 30s; # 有效期內文件最少使用次數 open_file_cache_min_uses 2; #xbq add #upstream作負載均衡,在此配置需要輪詢的服務器地址和端口號,max_fails為允許請求失敗的次數,默認為1. #weight為輪詢權重,根據不同的權重分配可以用來平衡服務器的訪問率。 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; #下面三條指令允許重新定義和添加一些將被轉移到被代理服務器的請求頭部信息 # 請求頭中Host信息 proxy_set_header Host $host; # 真實的客戶端IP proxy_set_header X-Real-IP $remote_addr; # 代理路由信息,此處取IP有安全隱患 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 真實的用戶訪問協(xié)議 proxy_set_header X-Forwarded-Proto $scheme; # 默認值default, # 后端response 302時 tomcat header中l(wèi)ocation的host是http://192.168.1.62:8080 # 因為tomcat收到的請求是nginx發(fā)過去的, nginx發(fā)起的請求url host是http://192.168.1.62:8080 # 設置為default后,nginx自動把響應頭中l(wèi)ocation host部分替換成當前用戶請求的host部分 # 網上很多教程將此值設置成 off,禁用了替換, # 這樣用戶瀏覽器收到302后跳到http://192.168.1.62:8080,直接將后端服務器暴露給瀏覽器 # 所以除非特殊需要,不要設置這種畫蛇添足的配置 proxy_redirect default; client_max_body_size 10m; #允許客戶端請求的最大單文件字節(jié)數 client_body_buffer_size 128k; #緩沖區(qū)代理緩沖用戶端請求的最大字節(jié)數 proxy_connect_timeout 90; #nginx跟后端服務器連接超時時間 proxy_read_timeout 90; #連接成功后,后端服務器響應時間 proxy_buffer_size 4k; #設置代理服務器(nginx)保存用戶頭信息的緩沖區(qū)大小 proxy_buffers 6 32k; #proxy_buffers緩沖區(qū),網頁平均在32k以下的話,這樣設置 proxy_busy_buffers_size 64k;#高負荷下緩沖大?。╬roxy_buffers*2) proxy_temp_file_write_size 64k; #設定緩存文件夾大小,大于這個值,將從upstream服務器傳 } #xbq add #配置Nginx動靜分離,定義的靜態(tài)頁面直接從/usr/nginxStaticFile(Nginx發(fā)布目錄)讀取。 location ~\.(gif|jpg|jpeg|png|css|js|php)$ { #expires定義用戶瀏覽器緩存的時間為7天,如果靜態(tài)頁面不常更新,可以設置更長,這樣可以節(jié)省帶寬和緩解服務器的壓力 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; # } #} }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Red Hat Linux,Apache2.0+Weblogic9.2負載均衡集群安裝配置
- 用apache和tomcat搭建集群(負載均衡)
- tomcat6_apache2.2_ajp 負載均衡加集群實戰(zhàn)分享
- 簡單測試Apache是如何完成負載均衡策略配置
- Apache實現Web Server負載均衡詳解(不考慮Session版)
- Apache2.2以上版本與Tomcat整合配置及負載均衡實現
- Apache負載均衡設置方法 mod_proxy使用介紹
- apache負載均衡的安裝和實現方法
- Nginx+Tomcat高性能負載均衡集群搭建教程
- Nginx+Tomcat搭建高性能負載均衡集群的實現方法
- nginx+tomcat實現Windows系統(tǒng)下的負載均衡搭建教程
- 詳解基于Centos7+Nginx+Tomcat8的負載均衡服務器的搭建
- 詳解Nginx + Tomcat 反向代理 負載均衡 集群 部署指南
- nginx+tomcat實現負載均衡,使用redis session共享
- Nginx與Tomcat實現動靜態(tài)分離和負載均衡
- Nginx+Tomcat+Https 服務器負載均衡配置實踐方案詳解
- linux下Nginx+Tomcat負載均衡配置方法
- windows下nginx+tomcat配置負載均衡的方法
- Debian下搭建Nginx和Tomcat服務器實現負載均衡的方案
- Windows下Apache+Tomcat7負載均衡配置方法詳解
相關文章
PHP(FastCGI)在Nginx的alias下出現404錯誤的解決方法
這篇文章主要介紹了PHP(FastCGI)在Nginx的alias下出現404錯誤的解決方法,涉及nginx平臺的相關配置技巧,需要的朋友可以參考下2016-05-05Ubuntu?22.04.1?LTS?編譯安裝?nginx-1.22.1的配置過程
Ubuntu安裝Nginx有兩種方式,一種是通過命令的方式,這種方式安裝的Nginx版本低,之前漏掃掃出來Nginx版本低,需要升級所以現在用編譯的方式安裝版本高點的,本文介紹Ubuntu22.04.1?LTS編譯安裝nginx1.22.1的配置過程,本文給大家介紹的非常詳細,需要的朋友參考下吧2024-01-01Windows?Server?Nginx?反向代理Spring?Boot配置無效報404未找到的問題
一個Spring?Boot的系統(tǒng),開發(fā)完成發(fā)布到Windows服務器里,使用nginx作為反向代理,修改刷新配置文件,nginx.conf,總是報錯404,這篇文章主要介紹了Windows?Server?Nginx?反向代理Spring?Boot配置無效?404?未找到的問題及解決方案2024-01-01windows下nginx的安裝使用及解決80端口被占用nginx不能啟動的問題
這篇文章主要給大家介紹了關于windows下nginx的安裝使用,以及如何解決80端口被占用導致nginx不能啟動的問題,文中介紹的非常詳細,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-04-04ConfigMap掛載與Subpath在Nginx容器中的應用小結
configmap可以通過ENV環(huán)境變量和文件兩種方式掛載到容器中,修改configmap后容器中對應的ENV環(huán)境變量不會更新,將配置文件nginx.conf以configmap文件的方式掛載到容器中,本文介紹ConfigMap掛載與Subpath在Nginx容器中的應用小結,感興趣的朋友一起看看吧2024-03-03