Nginx反向代理服务器配置基础教程

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Nginx反向代理服务器配置教程一,系统架构
二,Nginx安装
系统环境与组件:
Centos 6.6 x64
nginx v1.92
gcc
openssl-devel
pcre-devel
libevent
1,更换国内源
1)、下载新源及将本地源备份
# cd /etc/yum.repos.d/
# wget /.help/CentOS6-Base-163.repo
# mv CentOS-Base.repo CentOS-Base.repo.backup
# mv CentOS6-Base-163.repo CentOS-Base.repo
2)、yum源更新
# yum clean all && yum makecache && yum update -y
3)、完成
2,安装依赖包
1)
# yum -y install gcc
# yum -y install openssl openssl-devel
# yum -y install pcre-devel
检查安装结果命令:
# rpm -qa |grep gcc
2)安装libevent
libevent是一个事件触发的网络库,适用于windows、linux、bsd等多种平台,内部使用select、epoll、kqueue等系统调用管理事件机制。

著名分布式缓存软件memcached也是libevent based,而且libevent在使用上可以做到跨平台,而且根据libevent官方网站上公布的数据统计,似乎也有着非凡的性能。

官网下载地址:/
安装步骤:
# cd ~ #进入用户主目录,软件包都保存在这里。

# wget /project/levent/libevent/libevent-2.1/libevent-2.1.4-alpha.tar.gz
# tar –xvf libevent-2.1.4-alpha.tar.gz
# cd libevent-2.1.4-alpha
# ./configure
# make
# make install
3,相关用户及目录建立
1)建立运行nginx的用户与用户组
# groupadd -r nginx
# useradd -r -g nginx -s /bin/false -M nginx
2)建立安装nginx要用到的临时文件目录
#mkdir -p/var/tmp/nginx/proxy
4,Nginx安装
1)下载nginx-1.9.2.tar.gz,上传至/root目录。

# cd ~
# tar –xvf nginx-1.9.2.tar.gz
# cd nginx-1.9.2
/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-ht tp_gzip_static_module --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --with-pcre
# make && make install
参数释义:
--prefix=/usr
#安装完成将自动生成目录usr,而且该软件任何的文档都被复制到这个目录
--sbin-path=/usr/sbin/nginx Nginx
#可执行文件安装路径。

只能安装时指定,如果没有指定,默认为<prefix>/sbin/nginx。

--conf-path=/etc/nginx/nginx.conf
# 在没有给定-c选项下默认的nginx.conf的路径。

如果没有指定,默认为<prefix>/conf/nginx.conf。

--error-log-path=/var/log/nginx/error.log
# 在nginx.conf中没有指定error_log指令的情况下,默认的错误日志的路径。

如果没有指定,默认为<prefix>/logs/error.log。

--http-log-path=/var/log/nginx/access.log
# 在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。

如果没有指定,默认为<prefix>/logs/access.log。

--pid-path=/var/run/nginx/nginx.pid
# 在nginx.conf中没有指定pid指令的情况下,默认的nginx.pid的路径。

如果没有指定,默认为<prefix>/logs/nginx.pid。

--lock-path=/var/lock/nginx.lock
# nginx.lock文件的路径。

--user=nginx
# 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。

如果没有指定,默认为nobody。

--group=nginx
# 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的组。

如果没有指定,默认为nobody。

--with-http_ssl_module
# 开启HTTP SSL模块,使NGINX可以支持HTTPS请求。

这个模块需要已经安装了OPENSSL,在DEBIAN上是libssl --with-http_flv_module
# 启用ngx_http_flv_module,模块ngx_http_flv_module为Flash Video(FLV)文件提供服务端伪流媒体支持
--with-http_stub_status_module
# 启用"server status" 页
--with-http_gzip_static_module
# 启用ngx_http_gzip_module,静态压缩
--http-proxy-temp-path=/var/tmp/nginx/proxy/
# 设置路径http代理临时文件
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/
# 设置路径的http FastCGI的临时文件
--with-pcre
# 启用正则表达式及rewrite模块
安装完毕记住以下路径:
nginx path prefix: "/usr"
nginx binary file: "/usr/sbin/nginx"
nginx configuration prefix: "/etc/nginx"
nginx configuration file: "/etc/nginx/nginx.conf"
nginx error log file: "/var/log/nginx/error.log"
nginx http access log file: "/var/log/nginx/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "/var/tmp/nginx/proxy/"
nginx http fastcgi temporary files: "/var/tmp/nginx/fcgi/"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
注意:后面的各种配件文件中要用到的路径都与这里保持一致。

5,运行nginx测试网站
1)运行nginx服务
# nginx
2)打开http://192.168.1.86,如果能看到如下图,那么配置成功;
3)如果打不开,可能是防火墙端口没开,可以关闭防火墙后试一下,具体如何开启80端口,本教程不做介绍;# service iptables stop
三,配置nginx反向代理服务
1、修改nginx服务配置文件:
# vi /etc/nginx/nginx.conf
配置文件内容如下:
######################################################################
# 设置运行nginx的用户
user nginx nginx;
# 这里设置为当前CPU核心数
worker_processes 1;
# nginx运行日志
error_log /var/log/nginx/error.log;
# nginx运行进程ID存储路径
pid /var/run/nginx/nginx.pid;
# 每个nginx进程打开文件描述符最大数目
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
# 站点配置文件路径
include /etc/nginx/conf.d/*.conf;
# 开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。

注意:如果图片显示不正常把这个改成off
sendfile on;
# 长连接超时时间,单位是秒
keepalive_timeout 120;
# 开启gzip压缩输出
gzip on;
# 缓冲区代理缓冲用户端请求的最大字节数,可以理解为保存到本地再传给用户
client_max_body_size 50m;
# 缓冲区代理缓冲用户端请求的最大字节数
client_body_buffer_size 256k;
# 设置请求头的超时时间
client_header_timeout 10;
# 设置请求体的超时时间
client_body_timeout 10;
# 指定客户端的响应超时时间
send_timeout 10;
# nginx跟后端服务器连接超时时间(代理连接超时)
proxy_connect_timeout 300;
# 连接成功后,后端服务器响应时间(代理接收超时)
proxy_read_timeout 300;
# 后端服务器数据回传时间(代理发送超时)
proxy_send_timeout 300;
# 设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffer_size 64k;
# proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_buffers 4 32k;
# 高负荷下缓冲大小(proxy_buffers*2)
proxy_busy_buffers_size 64k;
# 设定缓存文件夹大小,大于这个值,将从upstream服务器传递请求,而不缓冲到磁盘proxy_temp_file_write_size 64k;
# 不允许代理端主动关闭连接
proxy_ignore_client_abort on;
}
######################################################################
2、新建站点配置文件/etc/nginx/conf.d/govpf.conf
# mkdir –p /etc/nginx/conf.d
# vi /etc/nginx/conf.d/govpf.conf
配置文件内容如下:
######################################################################
#192.168.1.85
server
{
listen 80;
# 设置站点域名
server_name ;
location / {
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#被转发地址及端口号
proxy_pass http://192.168.1.85:8001;
}
# 运行日志设置
access_log /var/log/nginx/_access.log;
}
######################################################################
注意:如果有多个站点,可以将以上内容复制后添加到配置末尾即可,或者另外新建一个任意名称的配置文件单独存放也行。

配置完成后可以运行nginx –t来检查配置文件是否有错误。

四,将nginx创建为服务并加入开机启动
1)新建启动脚本
# cd ~
# vi nginx
新建启动脚本内容如下:
######################################################################
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
# 设置nginx运行路径
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
sysconfig="/etc/sysconfig/$prog"
lockfile="/var/lock/subsys/nginx"
# 设置nginx进程ID路径,这里容易设错,注意一定要跟之前nginx.conf里设置的路径一致pidfile="/var/run/nginx/nginx.pid"
# 设置nginx配置文件路径
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f $sysconfig ] && . $sysconfig
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 -p $pidfile $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest_q || return 6
stop
start
}
reload() {
configtest_q || return 6
echo -n $"Reloading $prog: "
killproc -p $pidfile $prog -HUP
echo
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
configtest_q() {
$nginx -t -q -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
# Upgrade the binary with no downtime.
upgrade() {
local oldbin_pidfile="${pidfile}.oldbin"
configtest_q || return 6
echo -n $"Upgrading $prog: "
killproc -p $pidfile $prog -USR2
retval=$?
sleep 1
if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]]; then killproc -p $oldbin_pidfile $prog -QUIT
success $"$prog online upgrade"
echo
return 0
else
failure $"$prog online upgrade"
echo
return 1
fi
}
# Tell nginx to reopen logs
reopen_logs() {
configtest_q || return 6
echo -n $"Reopening $prog logs: "
killproc -p $pidfile $prog -USR1
retval=$?
echo
return $retval
}
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest|reopen_logs)
$1
;;
force-reload|upgrade)
rh_status_q || exit 7
upgrade
;;
reload)
rh_status_q || exit 7
$1
;;
status|status_q)
rh_$1
;;
condrestart|try-restart)
rh_status_q || exit 7
restart
;;
*)
echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}"
exit 2
esac
######################################################################
2)创建服务
# cp nginx /etc/init.d/nginx
# chmod 775 /etc/init.d/nginx
# cd /etc/init.d/
# chkconfig –add nginx
# chkconfig nginx on
3) 测试服务
# pkill nginx # 停止nginx进程
# service nginx start #启动服务
# service nginx stop #停止服务
# service nginx restart #重启服务
以上测试都不报错的话说明配置正常。

4)最后一步,测试是否转发正常,如果不正常,请检查/etc/nginx/conf.d下的配置文件。

常州网管员论坛
李老实
2015-06-26。

相关文档
最新文档