标签:nginx反向代理
nginx有着优秀的代理性能,很多情况下,nginx常常被充当反向代理服务器负载后端应用web构建起一个高性能高可用的web集群(淘宝tengix ,京东的nginx集群都使用到了nginx反向代理功能),接下来给大家讲解Linux平台部署nginx反向代理实例。
【本文档所介绍的内容适用于公司测试/生产等常见的nginx反向代理应用】
1. nginx环境部署前准备:
1.1相关软件以及系统
系统要求:Centos 6.0以上 (64位)
相关中间件:Nginx: 1.6.0 以上(包含1.6.0)
1.2相关系统依赖包安装检查准备
1.2.1 检查系统自带httpd,mysql是否安装
# rpm -qa | grep nginx
如有安装,请使用以下命令卸载相关程序
# yum remove nginx
2. 编译安装Nginx
在正式编译httpd时,首先需要下载Nginx以及安装编译nginx需要的依赖包
这里版本以1.6.3为例
2.1安装编译nginx需要的依赖包(默认包放在/root目录下,包统一解压到/usr/local/src)
# yum install gcc openssl-devel pcre-devel zlib-devel -y
2.2下载nginx并添加运行nginx服务账号(默认包放在/root目录下,包统一解压到/usr/local/src)
# wget http://nginx.org/download/nginx-1.6.3.tar.gz # groupadd -r nginx # useradd -r -g nginx -s /bin/false -M nginx
2.3 编译安装nginx
# cd ~
# tar -zxf /root/nginx-1.6.3.tar.gz -C /usr/local/src
# cd /usr/local/src/nginx-1.6.3
# ./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nignx \
--group=nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi/ \
--http-scgi-temp-path=/var/tmp/nginx/scgi/ \
--with-pcre
# make && make install
2.4 创建nginx相关缓存存放的目录以及启动服务脚本
2.4.1 创建nginx相关缓存存放的目录
mkdir -p /var/tmp/nginx
2.4.2 创建nginx服务启动脚本并赋予执行权限
vim /etc/init.d/nginx 内容如下: #!/bin/sh # # nginx - this script starts and stops the nginx daemin # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse # proxy and IMAP/POP3 proxy server # processname: nginx # config: /usr/local/nginx/conf/nginx.conf # pidfile: /usr/local/nginx/logs/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" lockfile=/var/lock/nginx.lock start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force- reload|configtest}" exit 2 esac
2.5 参考以下模板修改nginx主配置文件nginx.conf,如下所示
#user nobody; user nginx ; worker_processes auto; #worker_cpu_affinity 00000001 00000010 00000100 00001000 worker_rlimit_nofile 65535; #error_log logs/error.log info; #pid logs/nginx.pid; events { use epoll; worker_connections 65535; } http { include mime.types; include /etc/nginx/web.conf; 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; # {nginx global setting} charset utf-8; server_names_hash_bucket_size 128; client_header_buffer_size 1M; # client_body_timeout 15; # client_header_timeout 15; # send_timeout 15; large_client_header_buffers 4 128k; client_max_body_size 2000m; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 60; reset_timedout_connection on; # {fastcgi setting} fastcgi_cache_path /var/tmp/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m max_size=10g; fastcgi_connect_timeout 90; fastcgi_send_timeout 60; fastcgi_read_timeout 60; fastcgi_buffer_size 64k; fastcgi_buffers 8 128k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; # fastcgi_temp_path /usr/local/nginx/ngx_fastcgi_tmp # {file setting} open_file_cache max=204800 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; # open_file_cache_errors on; # {gzip setting} gzip on; # gzip_disable "msie6"; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_min_length 1k; gzip_buffers 16 8k; gzip_http_version 1.1; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; # {proxy setting} proxy_max_temp_file_size 0; proxy_connect_timeout 90; proxy_send_timeout 200; proxy_read_timeout 200; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; #access_log logs/access.log main; # sendfile on; #tcp_nopush on; #keepalive_timeout 0; # keepalive_timeout 65; #gzip on; # server { # listen 80; # server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; # location / { # root html; # index index.html index.htm; # } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # # error_page 500 502 503 504 /50x.html; # location = /50x.html { # root html; # } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache‘s document root # concurs with nginx‘s one # #location ~ /\.ht { # deny all; #} # } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
2.6 新建一个web站点模板(vim /etc/nginx/web.conf),内容如下:
server { listen 80 default backlog=65535; server_name localhost; root /usr/local/www; index index.php index.html ; location / { proxy_pass http://localhost:81; 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; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; } # location ~ \.php$ { # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_split_path_info ^(.+\.php)(.*)$; # fastcgi_param PATH_INFO $fastcgi_path_info; # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # include fastcgi_params; # fastcgi_cache cache; # fastcgi_cache_valid 200 302 1h; # fastcgi_cache_valid any 1m; # fastcgi_cache_min_uses 1; # fastcgi_cache_use_stale error timeout invalid_header http_500 http_503 http_404; # fastcgi_cache_key "$request_method://$host$request_uri"; # } }
注:述上注释的部分根据自己的需求来选择,如需要nginx支持php解析,去掉注释即可
2.7 检查nginx配置是否正确并启动nginx服务
2.7.1检查nginx配置文件是否有问题
# /usr/local/nginx/sbin/nginx -t 如出现以下信息说明配置无误 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
2.7.2 启动nginx服务并加入开机自启动服务中
# chmod o=rwx /etc/init.d/nginx //赋予ngin启动脚本执行权限 # service nginx start # chkconfig --add nginx # chkconfig --level 2345 nginx on
3. 验证
为了验证nginx是否成功反向代理后端web服务,需要在安装nginx这台服务器上再安装一个http服务(模拟后端web ),这里选择的是Apache(为避免与nginx混淆),如下所示:
# yum install httpd //安装后端web 服务 # vim /etc/httpd/conf/httpd.conf 找到Listen 80 这一行改为 Listen 81 # service httpd start
首先要知道nginx侦听的是80端口,Apache侦听的是81端口,所以如果nginx代理成功的,输入:http://ip访问应该是Apache的页面,如下所示(本文档中serverIP为:192.168.100.11):
若代理失败应该nginx报502错误:“bad gateway”!
本文出自 “菜鸟的成长记” 博客,请务必保留此出处http://blief.blog.51cto.com/6170059/1739328
标签:nginx反向代理
原文地址:http://blief.blog.51cto.com/6170059/1739328