Nginx ("engine x") 是一個(gè)高性能的 HTTP 和反向代理服務(wù)器,也是一個(gè) IMAP/POP3/SMTP 代理服務(wù)器。 Nginx 是由 Igor Sysoev 為俄羅斯訪問(wèn)量第二的 Rambler.ru 站點(diǎn)開(kāi)發(fā)的,它已經(jīng)在該站點(diǎn)運(yùn)行超過(guò)兩年半了。Igor 將源代碼以類BSD許可證的形式發(fā)布。
Nginx 的中文維基:http://wiki.codemongers.com/NginxChs
在高并發(fā)連接的情況下,Nginx是Apache服務(wù)器不錯(cuò)的替代品。Nginx同時(shí)也可以作為7層負(fù)載均衡服務(wù)器來(lái)使用。根據(jù)我的測(cè)試結(jié)果,Nginx 0.5.31 + PHP 5.2.4 (FastCGI) 可以承受3萬(wàn)以上的并發(fā)連接數(shù),相當(dāng)于同等環(huán)境下Apache的10倍。
Nginx 超越 Apache 的高性能和穩(wěn)定性,使得國(guó)內(nèi)使用 Nginx 作為 Web 服務(wù)器的網(wǎng)站也越來(lái)越多,其中包括新浪博客、新浪播客、網(wǎng)易新聞、騰訊網(wǎng)、搜狐博客等門戶網(wǎng)站頻道,六間房、56.com等視頻分享網(wǎng)站,Discuz!官方論壇、水木社區(qū)等知名論壇,盛大在線、金山逍遙網(wǎng)等網(wǎng)絡(luò)游戲網(wǎng)站,豆瓣、人人網(wǎng)、YUPOO相冊(cè)、金山愛(ài)詞霸、迅雷在線等新興Web 2.0網(wǎng)站。
在高并發(fā)連接的情況下,Nginx是Apache服務(wù)器不錯(cuò)的替代品。Nginx同時(shí)也可以作為7層負(fù)載均衡服務(wù)器來(lái)使用。根據(jù)我的測(cè)試結(jié)果,Nginx 0.8.46 + PHP 5.2.14 (FastCGI) 可以承受3萬(wàn)以上的并發(fā)連接數(shù),相當(dāng)于同等環(huán)境下Apache的10倍。
根據(jù)我的經(jīng)驗(yàn),4GB內(nèi)存的服務(wù)器+Apache(prefork模式)一般只能處理3000個(gè)并發(fā)連接,因?yàn)樗鼈儗⒄加?GB以上的內(nèi)存,還得為系統(tǒng)預(yù)留1GB的內(nèi)存。我曾經(jīng)就有兩臺(tái)Apache服務(wù)器,因?yàn)樵谂渲梦募性O(shè)置的MaxClients為4000,當(dāng)Apache并發(fā)連接數(shù)達(dá)到3800時(shí),導(dǎo)致服務(wù)器內(nèi)存和Swap空間用滿而崩潰。
而這臺(tái) Nginx 0.8.46 + PHP 5.2.14 (FastCGI) 服務(wù)器在3萬(wàn)并發(fā)連接下,開(kāi)啟的10個(gè)Nginx進(jìn)程消耗150M內(nèi)存(15M*10=150M),開(kāi)啟的64個(gè)php-cgi進(jìn)程消耗1280M內(nèi)存(20M*64=1280M),加上系統(tǒng)自身消耗的內(nèi)存,總共消耗不到2GB內(nèi)存。如果服務(wù)器內(nèi)存較小,完全可以只開(kāi)啟25個(gè)php-cgi進(jìn)程,這樣php-cgi消耗的總內(nèi)存數(shù)才500M。
在3萬(wàn)并發(fā)連接下,訪問(wèn)Nginx 0.8.46 + PHP 5.2.14 (FastCGI) 服務(wù)器的PHP程序,仍然速度飛快。下圖為Nginx的狀態(tài)監(jiān)控頁(yè)面,顯示的活動(dòng)連接數(shù)為28457(關(guān)于Nginx的監(jiān)控頁(yè)配置,會(huì)在本文接下來(lái)所給出的Nginx配置文件中寫明):
為什么Nginx的性能要比Apache高得多?這得益于Nginx使用了最新的epoll(Linux 2.6內(nèi)核)和kqueue(freebsd)網(wǎng)絡(luò)I/O模型,而Apache則使用的是傳統(tǒng)的select模型。目前Linux下能夠承受高并發(fā)訪問(wèn)的Squid、Memcached都采用的是epoll網(wǎng)絡(luò)I/O模型。
處理大量的連接的讀寫,Apache所采用的select網(wǎng)絡(luò)I/O模型非常低效。下面用一個(gè)比喻來(lái)解析Apache采用的select模型和Nginx采用的epoll模型進(jìn)行之間的區(qū)別:
假設(shè)你在大學(xué)讀書(shū),住的宿舍樓有很多間房間,你的朋友要來(lái)找你。select版宿管大媽就會(huì)帶著你的朋友挨個(gè)房間去找,直到找到你為止。而epoll版宿管大媽會(huì)先記下每位同學(xué)的房間號(hào),你的朋友來(lái)時(shí),只需告訴你的朋友你住在哪個(gè)房間即可,不用親自帶著你的朋友滿大樓找人。如果來(lái)了10000個(gè)人,都要找自己住這棟樓的同學(xué)時(shí),select版和epoll版宿管大媽,誰(shuí)的效率更高,不言自明。同理,在高并發(fā)服務(wù)器中,輪詢I/O是最耗時(shí)間的操作之一,select和epoll的性能誰(shuí)的性能更高,同樣十分明了。
某公司有一站點(diǎn),一天IP 430W,PV 3100W,之前采用5臺(tái) DELL R610 做NLB,系統(tǒng)2008 IIS7.5.每天高峰期時(shí)都不堪重負(fù).會(huì)出現(xiàn)以下情況
1:遠(yuǎn)程登陸巨慢無(wú)比
2:遠(yuǎn)程登陸系統(tǒng)后無(wú)法操作,表現(xiàn)為鼠標(biāo)移動(dòng)緩慢,操作延時(shí),系統(tǒng)呆滯.
3:CPU長(zhǎng)期在80%以上,內(nèi)存消耗極高
4:....
經(jīng)過(guò)部門討論后決定采用Nginx反向代理的架構(gòu)替代目前不堪重負(fù)的站點(diǎn).拓?fù)鋱D如下
硬件:
前端采用2臺(tái)DELL M420刀片,虛擬4臺(tái)Centos5.8跑Nginx反向代理.
后端依舊采用5臺(tái)服務(wù)器,不過(guò)從硬件上也升級(jí)到DELL M420 2008系統(tǒng) IIS7.5
網(wǎng)絡(luò)結(jié)構(gòu):
Nginx
電信 192.168.0.1 192.168.0.2
聯(lián)通 192.168.1.1 192.168.1.2
2008 IIS7.5
電信 192.168.0.10 192.168.0.11 192.168.0.13 192.168.0.14 192.168.0.15
聯(lián)通 192.168.1.10 192.168.1.11 192.168.1.13 192.168.1.14 192.168.1.15
域名xxx.com通過(guò)DNS輪詢到 Nginx前端,再根據(jù)你的配置甩向不同upstream
新架構(gòu)部署好后能明顯感覺(jué)整個(gè)站點(diǎn)打開(kāi)都飛快,后端完全無(wú)壓力狀態(tài),這還并沒(méi)有開(kāi)啟Nginx自帶的Cache.
現(xiàn)在這套架構(gòu)每天承載百萬(wàn)IP 千萬(wàn)PV的訪問(wèn)量 如圖
AWStats 統(tǒng)計(jì)結(jié)果
Nginx性能圖如下
單臺(tái)Centos CPU圖
安裝步驟:
一:
2臺(tái)宿主機(jī)虛擬成4臺(tái)這個(gè)步驟就略過(guò),網(wǎng)上教程一大把.在這里我們每臺(tái)分配2個(gè)cpu共8個(gè)核心,內(nèi)存6G,硬盤100G.
系統(tǒng)要求:Linux 2.6+ 內(nèi)核,本文中的Linux操作系統(tǒng)為CentOS 5.8 64位)
系統(tǒng)為最精簡(jiǎn)安裝文本界面,系統(tǒng)安裝好后配置系統(tǒng).
#修改系統(tǒng)DNS
rm -rf /etc/resolv.conf
touch /etc/resolv.conf
echo -ne "nameserver 114.114.114.114\nnameserver 114.114.115.115\nsearch localdomain" >>/etc/resolv.conf
#修改系統(tǒng)源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget http://mirrors.163.com/.help/CentOS5-Base-163.repo
cp CentOS5-Base-163.repo /etc/yum.repos.d/
#yum 升級(jí)安裝,根據(jù)需求自己行刪除
yum install -y lrzsz gcc gcc-c++ make flex autoconf automake vixie-cron libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel kernel-devel wget patch ntp libevent m4 zip unzip gd gd-devel file libtool-libs gmp-devel pspell-devel mod_perl-devel parted apr-util readline-devel sysstat vim* popt-devel patch openssh-clients net-snmp net-snmp-devel net-snmp-utils net-snmp-libs
#設(shè)置ssh下 SecureCRT標(biāo)簽顯示為帳號(hào)@IP
touch /etc/sysconfig/bash-prompt-xterm
vim /etc/sysconfig/bash-prompt-xterm
#添加以下一行
echo -ne "\e]2;${USER}@$(/sbin/ifconfig eth0|grep 'inet addr'|sed -e 's/^.*inet addr:\(.*\) Bcast.*$/\1/')\a"
#設(shè)置term類型是screen時(shí)(命令screen進(jìn)入新窗口時(shí))標(biāo)簽顯示內(nèi)容, 和xterm一樣
cp /etc/sysconfig/bash-prompt-xterm /etc/sysconfig/bash-prompt-screen
#設(shè)置term類型不是xterm和screen時(shí)標(biāo)簽顯示內(nèi)容, 也和xterm一樣
cp /etc/sysconfig/bash-prompt-xterm /etc/sysconfig/bash-prompt-default
#給予可執(zhí)行權(quán)限
chmod +x /etc/sysconfig/bash-prompt-*
#修改文件句柄數(shù)為65535,默認(rèn)系統(tǒng)為1024
echo "session required /lib64/security/pam_limits.so" >>/etc/pam.d/login
echo -ne "
* soft nofile 65534
* hard nofile 65534
" >>/etc/security/limits.conf
#添加iptables
rm -rf /etc/sysconfig/iptables
touch /etc/sysconfig/iptables
cat << EOF >> /etc/sysconfig/iptables
此段加入你的iptables規(guī)則
EOF
service iptables restart
#根據(jù)硬件優(yōu)化Linux性能
cat << EOF >> /etc/sysctl.conf
# Add
#開(kāi)啟SYN Cookies,當(dāng)出現(xiàn)SYN等待隊(duì)列溢出時(shí),啟用cookies來(lái)處理
net.ipv4.tcp_syncookies = 1
#表示SYN隊(duì)列的長(zhǎng)度,默認(rèn)為1024,加大隊(duì)列長(zhǎng)度為8192,可以容納更多等待連接的網(wǎng)絡(luò)連接數(shù)。
net.ipv4.tcp_max_syn_backlog = 65536
#每個(gè)網(wǎng)絡(luò)接口接收數(shù)據(jù)包的速率比內(nèi)核處理這些包的速率快時(shí),允許送到隊(duì)列的數(shù)據(jù)包的最大數(shù)目
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_fin_timeout = 30
#當(dāng)keepalive起用的時(shí)候,TCP發(fā)送keepalive消息的頻度。缺省是2小時(shí)
net.ipv4.tcp_keepalive_time = 1200
#redis設(shè)置
net.core.somaxconn = 262144
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
#參數(shù)的值決定了內(nèi)核放棄連接之前發(fā)送SYN+ACK包的數(shù)量
net.ipv4.tcp_synack_retries = 2
#在內(nèi)核放棄建立連接之前發(fā)送SYN包的數(shù)量
net.ipv4.tcp_syn_retries = 2
#啟用timewait快速回收
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
#系統(tǒng)中最多有多少個(gè)TCP套接字不被關(guān)聯(lián)到任何一個(gè)用戶文件句柄上。這個(gè)限制僅僅是為了防止簡(jiǎn)單的DoS攻擊,不能過(guò)分依靠它或者人為地減小這個(gè)值,更應(yīng)該增加這個(gè)值(如果增加了內(nèi)存之后)
net.ipv4.tcp_max_orphans = 3276800
#允許系統(tǒng)打開(kāi)的端口范圍
net.ipv4.ip_local_port_range = 1024 65535
#增加系統(tǒng)文件描述符限制
fs.file-max = 65535
#允許更多的PIDs (減少滾動(dòng)翻轉(zhuǎn)問(wèn)題); may break some programs 32768
kernel.pid_max = 65536
# 增加TCP最大緩沖區(qū)大小
net.ipv4.tcp_rmem = 4096 4096 16777216
net.ipv4.tcp_wmem = 4096 4096 16777216
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth0.send_redirects = 0
net.ipv4.conf.all.send_redirects = 0
#2012-8-30
#net.ipv4.ip_conntrack_max=1048576
#net.ipv4.netfilter.ip_conntrack_max=1048576
net.ipv4.netfilter.ip_conntrack_tcp_timeout_time_wait=120
net.ipv4.netfilter.ip_conntrack_tcp_timeout_close_wait=60
net.ipv4.netfilter.ip_conntrack_tcp_timeout_fin_wait=120
net.ipv4.neigh.default.gc_thresh1=10240
net.ipv4.neigh.default.gc_thresh2=40960
net.ipv4.neigh.default.gc_thresh3=81920
#timewait的數(shù)量
net.ipv4.tcp_max_tw_buckets =6000
EOF
#刷新sysctl.conf
復(fù)制代碼代碼如下:
/sbin/sysctl -p
#禁用ipv6
復(fù)制代碼代碼如下:
echo -ne "alias net-pf-10 off\noptions ipv6 disable=1" >>/etc/modprobe.conf
/sbin/chkconfig --level 35 ip6tables off
#關(guān)閉 selinux
復(fù)制代碼代碼如下:
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
#將同步好的時(shí)間寫到CMOS里
復(fù)制代碼代碼如下:
sed -i 's#SYNC_HWCLOCK=no#SYNC_HWCLOCK=yes#' /etc/sysconfig/ntpd
#設(shè)置同步時(shí)間
復(fù)制代碼代碼如下:
echo "0 6 * * * /usr/sbin/ntpdate 192.168.0.172;/sbin/hwclock -w; >/dev/null 2>&1" >>/var/spool/cron/root
service crond restart
#修改系統(tǒng)啟動(dòng)級(jí)別
復(fù)制代碼代碼如下:
sed -i 's/id:5:initdefault:/id:3:initdefault:/g' /etc/inittab
#禁止ctrl+alt+del
復(fù)制代碼代碼如下:
sed -i "s/ca::ctrlaltdel:\/sbin\/shutdown -t3 -r now/#ca::ctrlaltdel:\/sbin\/shutdown -t3 -r now/" /etc/inittab
二:
獲取相關(guān)開(kāi)源程序:
Nginx 官方當(dāng)前穩(wěn)定版為 nginx-1.2.4 ,我測(cè)試環(huán)境使用版本為nginx-1.3.3
pcre-8.31
nginx_upstream_check_module
ngx_cache_purge-1.6
請(qǐng)自行從網(wǎng)絡(luò)下載
安裝步驟:
#新建WWW用戶組和WWW用戶
復(fù)制代碼代碼如下:
groupadd www
useradd -g www www -s /bin/false
#創(chuàng)建相應(yīng)的目錄
復(fù)制代碼代碼如下:
mkdir -p /data/html/www
mkdir -p /data/proxy_temp_dir
mkdir -p /data/proxy_cache_path
mkdir -p /data/logs
chown -R www:www /data/proxy_cache_path
chown -R www:www /data/proxy_temp_dir
chown -R www:www /data/html/
chown -R www:www /data/logs
#安裝內(nèi)核和openssl
復(fù)制代碼代碼如下:
yum -y install openssl-devel kernel-devel
#安裝Pcre
復(fù)制代碼代碼如下:
tar zxvf pcre-8.31.tar.gz
cd pcre-8.31
./configure --prefix=/usr/local/pcre
make
make install
#安裝nginx
復(fù)制代碼代碼如下:
tar zxvf ngx_cache_purge-1.6.tar.gz
tar zxvf nginx-1.3.3.tar.gz
tar zxvf nginx_upstream_check_module.tar.gz
cd nginx-1.3.3
patch -p1 < /root/nginx_keepalived_install/nginx_upstream_check_module/check_1.2.2+.patch
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_realip_module --with-http_gzip_static_module --with-openssl=/usr/ --with-pcre=/root/nginx_keepalived_install/pcre-8.31 --add-module=/root/nginx_keepalived_install/ngx_cache_purge-1.6 --add-module=/root/nginx_keepalived_install/nginx_upstream_check_module
make
make install
cp /root/nginx_keepalived_install/cut_nginx_log.sh /usr/local/nginx/sbin/
chmod +x /usr/local/nginx/sbin/cut_nginx_log.sh
echo -ne "00 00 * * * /bin/bash /usr/local/nginx/sbin/cut_nginx_log.sh" >>/var/spool/cron/root
service crond restart
chown -R www:www /usr/local/nginx/
cp /root/nginx_keepalived_install/nginx /etc/rc.d/init.d/
chmod +x /etc/rc.d/init.d/nginx
chkconfig nginx on
到這里Nginx安裝完畢,目前Nginx所有的配置都是默認(rèn)配置,我貼下我案例中的NGINX配置,如有錯(cuò)誤請(qǐng)大牛們指導(dǎo).
Nginx.conf
復(fù)制代碼代碼如下:
user www www;
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_rlimit_nofile 204800;
error_log /data/logs/error.log;
pid logs/nginx.pid;
events {
worker_connections 240800;
use epoll;
}
http {
include nginx_load_balance.conf;
#include proxy.conf;
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
#log_format access '$remote_addr - $remote_user [$time_local] "$request" '
#'$status $body_bytes_sent "$http_referer" '
#'"$http_user_agent" $http_x_forwarded_for';
#access_log /data/logs/access.log access;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
keepalive_timeout 60;
client_header_buffer_size 4k;
#large_client_header_buffers 4 32k;
client_max_body_size 10m;
client_header_timeout 120s;
client_body_timeout 120s;
send_timeout 120s;
gzip off;
gzip_min_length 1k;
gzip_buffers 4 16k;
output_buffers 1 512k;
postpone_output 1460;
#gzip_http_version 1.1;
gzip_comp_level 4;
gzip_types text/plain text/css text/javascript text/xml application/x-javascript application/xml application/xml+rss;
gzip_vary on;
server {
listen 80;
server_name localhost;
location / {
rewrite ^ http://www.test.com$request_uri?;
#root /data/html/www;
#index index.html index.htm;
}
error_page 404 /data/html/www/404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /status {
stub_status on;
access_log off;
}
location /nstatus {
check_status;
access_log off;
#allow SOME.IP.ADD.RESS;
#deny all;
}
}
include vhost/*.conf;
}
nginx_load_balance.conf
復(fù)制代碼代碼如下:
upstream dx {
ip_hash;
server 192.168.0.11:80;
server 192.168.0.12:80;
server 192.168.0.13:80;
server 192.168.0.14:80;
server 192.168.0.15:80;
check interval=3000 rise=2 fall=8 timeout=3000 type=http;
check_http_send "GET / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx http_4xx;
#server 192.168.0.30:80; backup;
}
upstream lt {
ip_hash;
server 192.168.1.11:80;
server 192.168.1.12:80;
server 192.168.1.13:80;
server 192.168.1.14:80;
server 192.168.1.15:80;
check interval=3000 rise=2 fall=8 timeout=3000 type=http;
check_http_send "GET / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx http_4xx;
#server 192.168.1.30:80; backup;
}
proxy.conf
復(fù)制代碼代碼如下:
proxy_temp_path /data/proxy_temp_dir; #指定臨時(shí)文件目錄
proxy_cache_path /data/proxy_cache_path levels=1:2 keys_zone=cache_one:2000m inactive=1d max_size=1g;
client_body_buffer_size 512k; #增加緩沖區(qū)代理緩沖客戶端請(qǐng)求的最大字節(jié)數(shù)
proxy_connect_timeout 60; #增加連接后端服務(wù)器超時(shí)時(shí)間
proxy_read_timeout 60; #增加后端服務(wù)器響應(yīng)請(qǐng)求超時(shí)時(shí)間
proxy_send_timeout 60; #增加后端服務(wù)器發(fā)送數(shù)據(jù)超時(shí)時(shí)間
proxy_buffer_size 32k; #增加代理請(qǐng)求緩存區(qū)大小
proxy_buffers 4 64k; #增加
proxy_busy_buffers_size 128k; #增加系統(tǒng)繁忙時(shí)可申請(qǐng)的proxy_buffers大小
proxy_temp_file_write_size 128k; #增加proxy緩存臨時(shí)文件的大小
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404; #增加故障轉(zhuǎn)移,如果后端的服務(wù)器返回502、504、執(zhí)行超時(shí)等錯(cuò)誤,自動(dòng)將請(qǐng)求轉(zhuǎn)發(fā)到upstream負(fù)載均衡池中的另一臺(tái)服務(wù)器,實(shí)現(xiàn)故障轉(zhuǎn)移。
proxy_cache cache_one; #增加使用web緩存區(qū)cache_one
以上是Nginx配置,下面是vhost中網(wǎng)站配置
xxxx_com.conf
復(fù)制代碼代碼如下:
server
{
listen 192.168.0.1;
server_name xxx.com;
access_log /data/logs/dx.log dx;
location /
{
proxy_next_upstream http_502 http_504 error timeout invalid_header;
# proxy_cache cache_one;
# proxy_cache_valid 200 304 12h;
# proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $http_host;
#proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
proxy_pass http://dx;
#if ($request_uri ~* "^/upload/") {
#add_header Cache-Control 'max-age=86400';
#}
location ~* \.(gif|jpg|jpeg|png|flv|swf|ico)$
{
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://dx;
add_header Cache-Control 'max-age=86400';
}
location ~* \.(js)$
{
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://dx;
add_header Cache-Control 'max-age=600';
}
#expires -1;
add_header Cache-Control 'no-store, no-cache, must-revalidate';
add_header Pragma no-cache;
}
location ~ /purge(/.*)
{
allow 127.0.0.1;
allow 192.168.0.0/16;
deny all;
#proxy_cache_purge cache_one $host$1$is_args$args;
}
location ~ .*\.(php|jsp|cgi|asp|aspx|flv|swf|xml)?$
{
proxy_set_header Host $http_host;
#proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://dx;
}
}
server
{
listen 192.168.1.1;
server_name xxxx.com;
access_log /data/logs/lt.log lt;
location /
{
proxy_next_upstream http_502 http_504 error timeout invalid_header;
#proxy_cache cache_one;
#proxy_cache_valid 200 304 12h;
#proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $http_host;
#proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
proxy_pass http://lt;
#if ($request_uri ~* "^/upload/") {
#add_header Cache-Control 'max-age=86400';
#}
location ~* \.(gif|jpg|jpeg|png|flv|swf|ico)$
{
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://lt;
add_header Cache-Control 'max-age=86400';
}
location ~* \.(js)$
{
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://lt;
add_header Cache-Control 'max-age=600';
}
#expires -1;
add_header Cache-Control 'no-store, no-cache, must-revalidate';
add_header Pragma no-cache;
}
location ~ /purge(/.*)
{
allow 127.0.0.1;
allow 192.168.0.0/16;
deny all;
#proxy_cache_purge cache_one $host$1$is_args$args;
}
location ~ .*\.(php|jsp|cgi|asp|aspx|flv|swf|xml)?$
{
proxy_set_header Host $http_host;
#proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://lt;
}
}
至此,一套完整的Nginx反向代理全部結(jié)束.
網(wǎng)站無(wú)法打開(kāi)排查
1:ping域名看解析是否正常且是否連通.
2:配置本機(jī)Hosts指向后端單機(jī)IIS查看網(wǎng)站是否正常訪問(wèn).
3:檢查前端Nginx進(jìn)程和端口是否存在,
ps -ef |grep nginx
netstat -an |grep 80
系統(tǒng)相關(guān)配置
Nginx
Nginx目錄/usr/local/nginx/
/usr/local/nginx/conf/nginx.conf Nginx主配置文件
/usr/local/nginx/conf/nginx_load_balance.conf Nginx反向代理輪詢配置文件
/usr/local/nginx/conf/proxy.conf Nginx緩存配置相關(guān)
/usr/local/nginx/conf/vhost/ Nginx虛擬主機(jī)配置目錄
Snmp
/etc/snmp/snmpd.conf
日志切割
/root/scripts/ 所有日志切割腳本都在這個(gè)目錄
crontab -l 列出計(jì)劃任務(wù)
crontab -e 編輯計(jì)劃任務(wù)
系統(tǒng)相關(guān)操作
Nginx
重啟Nginx service nginx restart
重載Nginx配置文件 service nginx reload
檢測(cè)Nginx配置語(yǔ)法是否正確 /usr/local/nginx/sbin/nginx -t
關(guān)閉Nginx service nginx stop
啟動(dòng)Nginx service nginx start
Snmp
重啟Snmp service snmpd restart
關(guān)閉Snmp service snmpd stop
啟動(dòng)Snmp service snmpd start
最后編輯: chen 編輯于2012/11/07 17:57