Nginx配合Apache或Tomcat的動靜分離基本配置實(shí)例
其實(shí)本人比較喜歡nginx跑靜態(tài)和做負(fù)載反向代理,動態(tài)php還是交給apache處理比較穩(wěn)定,jsp就交給tomcat、resin或jboss。nginx跑靜態(tài)的能力是無與倫比的,是目前web服務(wù)器里最強(qiáng)的。nginx和apache、tomcat、resin的動靜分離配置其實(shí)很簡單,就幾句配置,穩(wěn)定性也非常好。
1、nginx和apache的動靜分離配置:
把下面配置放到nginx配置文件相應(yīng)的server { }里面,如果使用其他端口號,改一下就行:
#所有php的動態(tài)頁面均交由apache處理
location ~ \.(php)?$ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:88; }
#所有靜態(tài)文件由nginx直接讀取不經(jīng)過apache
location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { expires 15d; } location ~ .*\.(js|css)?$ { expires 1h; }
如果之前設(shè)置了FastCGI的,把下面的配置注釋掉:
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root /var/www/html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # include fastcgi.conf; #}
重啟nginx就生效,如圖所示,標(biāo)頭顯示nginx,phpinfo里面顯示是apache,說明動靜分離生效。
2.niginx和tomcat的動靜分離配置:
#主配置文件配置
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
user nginx; worker_processes 1; error_log logs/error.log; pid logs/nginx.pid; events { 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; sendfile on; keepalive_timeout 65; #gzip壓縮功能設(shè)置 gzip on; gzip_min_length 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; server { listen 80; server_name www.test.com; location / { #jsp網(wǎng)站程序根目錄,一般nginx與tomcat在同一個目錄 root /usr/local/tomcat/webapps/ROOT; index index.html index.jsp index.html; } location ~ .*.jsp$ { index index.jsp; proxy_pass http://127.0.0.1:8080; #來自jsp請求交給tomcat處理 proxy_redirect off; proxy_set_header Host $host; #后端的Web服務(wù)器可以通過X-Forwarded-For獲取用戶真實(shí)IP proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; #允許客戶端請求的最大單文件字節(jié)數(shù) client_body_buffer_size 128k; #緩沖區(qū)代理緩沖用戶端請求的最大字節(jié)數(shù) proxy_connect_timeout 90; #nginx跟后端服務(wù)器連接超時時間(代理連接超時) proxy_read_timeout 90; #連接成功后,后端服務(wù)器響應(yīng)時間(代理接收超時) 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ù)荷下緩沖大?。╬roxy_buffers*2) proxy_temp_file_write_size 64k; #設(shè)定緩存文件夾大小,大于這個值,將從upstream服務(wù)器傳 } location ~ .*\.(gif|jpg|png|bmp|swf)$ #由nginx處理靜態(tài)頁面 { expires 30d; #使用expires緩存模塊,緩存到客戶端30天 } location ~ .*\.( jsp|js|css)?$ { expires 1d; } error_page 404 /404.html; #錯誤頁面 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
#編寫nginx啟動、停止、重啟等SysV管理腳本,方便使用
[root@localhost ~]# vi /etc/init.d/nginx
#!/bin/bash # chkconfig: 345 99 20 # description: Nginx servicecontrol script PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG echo "Nginx servicestart success." ;; stop) kill -s QUIT $(cat $PIDF) echo "Nginx service stopsuccess." ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) echo"reload Nginx configsuccess." ;; *) echo "Usage: $0{start|stop|restart|reload}" exit 1 esac
[root@localhost ~]# chmod +x /etc/init.d/nginx [root@localhost ~]# service nginx restart [root@localhost ~]# chkconfig --add nginx [root@localhost ~]# chkconfig nginx on
- nginx實(shí)現(xiàn)tomcat動靜分離詳解
- 簡單實(shí)現(xiàn)nginx+tomcat的反向代理與動靜分離
- 使用Nginx+uWsgi實(shí)現(xiàn)Python的Django框架站點(diǎn)動靜分離
- nginx實(shí)現(xiàn)負(fù)載均衡和動靜分離
- nginx實(shí)現(xiàn)動靜分離實(shí)例講解
- Nginx實(shí)現(xiàn)動靜分離的示例代碼
- nginx實(shí)現(xiàn)動靜分離的方法示例
- 部署Nginx+Apache動靜分離的實(shí)例詳解
- Nginx+Tomcat實(shí)現(xiàn)負(fù)載均衡、動靜分離的原理解析
- 使用nginx實(shí)現(xiàn)動靜分離
相關(guān)文章
nginx+apache+mysql+php+memcached+squid搭建集群web環(huán)境
當(dāng)前,LAMP開發(fā)模式是WEB開發(fā)的首選,如何搭建一個高效、可靠、穩(wěn)定的WEB服務(wù)器一直是個熱門主題,本文就是這個主題的一次嘗試。2011-03-03nginx禁止直接通過ip進(jìn)行訪問并跳轉(zhuǎn)到自定義500頁面的操作
這篇文章主要介紹了nginx禁止直接通過ip進(jìn)行訪問并跳轉(zhuǎn)到自定義500頁面的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05nginx配置location總結(jié)location正則寫法及rewrite規(guī)則寫法
本文詳細(xì)講述了Nginx location正則寫法,Nginx 的Rewrite規(guī)則以及Nginx.conf中if指令與全局變量2018-10-10Nginx搭建RTMP流媒體服務(wù)器的實(shí)現(xiàn)
本文主要介紹了Nginx搭建RTMP流媒體服務(wù)器的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Nginx(PHP/fastcgi)的PATH_INFO問題
PATH_INFO是一個CGI 1.1的標(biāo)準(zhǔn),經(jīng)常用來做為傳參載體. 比如, 我們可以使用PATH_INFO來代替Rewrite來實(shí)現(xiàn)偽靜態(tài)頁面, 另外不少PHP框架也使用PATH_INFO來作為路由載體.2011-08-08教你快速構(gòu)建一個基于nginx的web集群項(xiàng)目
本文教你快速構(gòu)建一個基于nginx的4/7層負(fù)載均衡的web集群項(xiàng)目,項(xiàng)目步驟本文給大家介紹介紹,對nginx web集群項(xiàng)目感興趣的朋友一起看看吧2021-11-11Nginx反向代理與負(fù)載均衡實(shí)戰(zhàn)篇
這篇文章主要介紹了Nginx反向代理與負(fù)載均衡實(shí)戰(zhàn)篇,需要的朋友可以參考下2019-03-03