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
我的个人见解,忘大家多多指教
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论