安装
版本:centos7,nginx1.8.1
一、安装编译工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
二、首先要安装 PCRE
按照下面指令,依次下载 PCRE 安装包,解压安装包 ,进入安装包目录,编译安装 查看pcre版本。
1. wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz 2. tar zxvf pcre-8.35.tar.gz 3. cd pcre-8.35 4. ./configure 5. make && make install 6. pcre-config --version
三、安装 Nginx
按照下面指令,依此下载nginx安装包,解压,进入目录,编译安装,查看nginx版本。
1. wget http://nginx.org/download/nginx-1.8.1.tar.gz
2. tar zxvf nginx-1.8.1.tar.gz
3. cd nginx-1.8.1
4. ./configure --prefix=/usr/local/src/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
5. make
6. make install
#查看版本
7. /usr/local/src/nginx/sbin/nginx -v
编写启动脚本
安装完毕,编写启动脚本,方便我们快速启动nginx。
1. /etc/init.d/下 创建 nginx文件,编辑 vim /etc/init.d/nginx:
#! /bin/bash
# chkconfig: - 85 15
PATH=/usr/local/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0
2.更改权限:
chmod +x /etc/init.d/nginx
3.设置开机启动
/sbin/chkconfig nginx on
sudo /sbin/chkconfig --list nginx
nginx配置的时候,一般在nginx.conf配置一下基础配置,对于其它server模块配置,单独创建一个目录或文件配置,这样易于管理。
nginx基础配置都在nginx.conf文件下:
user nginx;#定义用户和组信息
worker_processes 4;#工作进程,一般和CPU核数相同
error_log logs/error.log;# 日志目录
#error_log logs/error.log info;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
pid logs/nginx.pid;#定义储存主进程的id目录,启动nginx会生成一个id保存到nginx.pid里
events { #与网络连接有关的模块
worker_connections 1024; #一个work进程同时处理的最大连接数,最大1024,默认512.
}
http { #http服务模块,所有和http有关的配置都在这里
include mime.types; #包含mime.types文件
default_type application/octet-stream; #定义默认的mime type
#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;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;#与客户端保持连接最大时间
#包含配置文件,为了方便管理,添加server,upstream模块配置,可以新建配置文件在一个目录下,用include指令包含进来
include /etc/nginx/conf.d/*.conf;
gzip on; #启用文件压缩
......
配置server模块,创建my.conf文件:
#
# The default server
#
server {
listen 8080 ;#监听接受请求服务器IP的地址和端口号,可以是主机名,localhost,端口号,或者*:8080
server_name 192.168.0.130; #服务器名ip或者域名
location ~ /BS { #与客户端请求的URI匹配,匹配/BFS
proxy_pass http://192.168.0.151:8781; #代理服务器协议和地址
proxy_redirect off ; #关闭重写访问地址
#proxy_set_header 允许重新定义或附加字段传递给代理服务器的请求头
proxy_set_header Host $host:8080; #传递客户端主机名
proxy_set_header X-Real-IP $remote_addr; #获取客户端真是IP地址
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ /MS {
proxy_pass http://ms;
# rewrite ^/ms/(.*)$ /$1 break;
proxy_redirect off ;
proxy_set_header Host $host:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#charset koi8-r;
#access_log logs/host.access.log main;
# Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
# location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
# }
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
#ms 系统配置了负载均衡,在upstream.conf中
upstream ms{
ip_hash; #轮询算法
server 192.168.0.152:9281; #上游服务器地址
server 1192.168.0.153:9281; #备用服务器
}