Nginx配合Apache或Tomcat的動靜分離基本配置實例
其實本人比較喜歡nginx跑靜態(tài)和做負載反向代理,動態(tài)php還是交給apache處理比較穩(wěn)定,jsp就交給tomcat、resin或jboss。nginx跑靜態(tài)的能力是無與倫比的,是目前web服務器里最強的。nginx和apache、tomcat、resin的動靜分離配置其實很簡單,就幾句配置,穩(wěn)定性也非常好。
1、nginx和apache的動靜分離配置:
把下面配置放到nginx配置文件相應的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直接讀取不經過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; }
如果之前設置了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就生效,如圖所示,標頭顯示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壓縮功能設置
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網站程序根目錄,一般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服務器可以通過X-Forwarded-For獲取用戶真實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跟后端服務器連接超時時間(代理連接超時)
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服務器傳
}
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+apache+mysql+php+memcached+squid搭建集群web環(huán)境
當前,LAMP開發(fā)模式是WEB開發(fā)的首選,如何搭建一個高效、可靠、穩(wěn)定的WEB服務器一直是個熱門主題,本文就是這個主題的一次嘗試。2011-03-03
nginx禁止直接通過ip進行訪問并跳轉到自定義500頁面的操作
這篇文章主要介紹了nginx禁止直接通過ip進行訪問并跳轉到自定義500頁面的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
nginx配置location總結location正則寫法及rewrite規(guī)則寫法
本文詳細講述了Nginx location正則寫法,Nginx 的Rewrite規(guī)則以及Nginx.conf中if指令與全局變量2018-10-10
Nginx(PHP/fastcgi)的PATH_INFO問題
PATH_INFO是一個CGI 1.1的標準,經常用來做為傳參載體. 比如, 我們可以使用PATH_INFO來代替Rewrite來實現(xiàn)偽靜態(tài)頁面, 另外不少PHP框架也使用PATH_INFO來作為路由載體.2011-08-08

