nginx反向代理並負載後端apache
nginx反向代理並負載後端apache
基於centos 5.4 x86-64
Nginx:192.168.1.100
Apache:192.168.1.135 192.168.1.136
一、安裝nginx
#/usr/sbin/groupadd www
#/usr/sbin/useradd -g www www
#tar zxvf pcre-8.00.tar.gz
#cd pcre-8.00
#./configure
#make && make install
# tar zxvf nginx-0.8.54.tar.gz
# cd nginx-0.8.54
# ./configure --user=www --group=www
--prefix=/usr/local/webserver/nginx --with-http_stub_status_module
--with-http_ssl_module
# make && make install
#vi /usr/local/webserver/nginx/conf/nginx.conf
修改為
user www;
worker_processes 4;
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http{
server_tokens off;
include mime.types;
default_type application/octet-stream;
access_log off;
error_log /dev/null;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
sendfile on;
tcp_nopush on;
keepalive_timeout 120;
tcp_nodelay on;
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-javascript text/css application/xml;
gzip_vary on;
upstream php_server_apache {
server 192.168.1.135:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.1.136:80 weight=1 max_fails=2 fail_timeout=30s;
}
include vhost/shop.yourdomain.conf;
}
#mkdir /usr/local/webserver/nginx/conf/vhost
#vim /usr/local/webserver/nginx/conf/vhost/shop.yourdomain.conf
server
{
listen 80;
server_name shop.yourdomain.com;
index index.html index.htm index.php;
root /data/www/app/shopmall/;
location /nginx {
stub_status on;
auth_basic "NginxStatus";
log_format access '$http_x_forwarded_for - $remote_user [$time_local] "$request"'
'$status $body_bytes_sent "$http_referer"'
'"$http_user_agent" $remote_addr';
access_log logs/shop.yourdomain.com_access.log access;}
location / {
location ~ .*\.(php|php5)?$ {
index index.php;
proxy_pass http://php_server_apache;
}
include proxy.conf;
if ( !-e $request_filename) {
proxy_pass http://php_server_apache;
}
location ~* \.(jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
root /data/www/app/shopmall/;
add_header X-Cache HIT;
expires 30d;
break;
}
}
location ~* \.( html|htm|js|css)$ {
if (-f $request_filename) {
root /data/www/app/shopmall/;
add_header X-Cache HIT;
expires 1d;
break;
}
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#vim /usr/local/webserver/nginx/conf/proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
#chown –R www.www /data/www
#chmod 755
-R /data/www
#chown –R /usr/local/webserver/nginx
#/usr/local/webserver/nginx/sbin/nginx
#echo 「/usr/local/webserver/nginx/sbin/nginx」 >> /etc/rc.local
訪問shop.yourdomain.com(k可以本地hosts解析到192.168.1.100) 試試吧………………
BUG問題解決………………..
(經親測可行)
Nginx和apache
搭配性能還是比較穩定的。但不乏有些bug問題要解決啊……….
①
Apache端獲得的IP問題
你會發現默認的話apache獲得的客戶端ip地址為nginx伺服器的IP
Apache端:
#vim httpd.conf
LoadModule rpaf_module modules/mod_rpaf-2.0.so
#vim httpd-vhosts.conf
在虛擬主機里加上
RPAFenable On
RPAFproxy_ips 192.168.1.100
RPAFsethostname On
RPAFheader X-Forwarded-For
Nginx端已經在proxy.conf里定義了
②
、session丟失
Nginx簡單輪訓會導致session丟失,面對這種問題,一種可以將nginx調度演算法換為ip_hash,這樣負載的作用將沒有意義,另一種可將session存在memcache里…..
#vim /usr/local/php/lib/php.ini
修改為
session.save_handler = memcache
session.save_path = "tcp://IP:11211"
③
、apache功能變更
由於apache只處理php,所以其自身多的功能也沒必要了,安裝像eaccelerator或xcache
優化緩存php
我的個人見解,忘大家多多指教
[火星人
]
nginx反向代理並負載後端apache已經有552次圍觀
http://coctec.com/docs/service/show-post-2374.html