安装nignx1.14,并且打补丁,反向代理健康监控,ssl,解决自启动报pid错误
//安装git,下载check_module的健康模块
yum install git git clone https://github.com/yaoweibin/nginx_upstream_check_module.git
// 上传并下载nginx源码,解压 tar xvf nginx-1.14.2.tar.gz
// 安装打补丁的工具 yum -y install patch
// 进入nginx源码目录,第一层
cd nginx-1.14.2
//打源码补丁
patch -p1 </root/nginx_upstream_check_module/check_1.14.0+.patch
// 以下为编译nginx所需的lib yum install gcc-c++ yum install -y pcre pcre-devel yum install -y zlib zlib-devel yum install -y openssl openssl-devel cd nginx-1.14.2
// 配置,安装目录为 /usr/nginx ,add-module 的目录为上面git后的目录 ./configure --prefix=/usr/nginx --with-http_ssl_module --add-module=/root/nginx_upstream_check_module/ make install
cd /usr/nginx cd sbin ./nginx
配置反向代理
建立upstream
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream yiwiki {
server 0.0.0.0:8080;
server 0.0.0.0:8090;
check interval=3000 rise=2 fall=5 timeout=2000 type=http;
check_http_expect_alive http_2xx http_3xx;
ip_hash;
}
server {
listen 80;
server_name localhost;
.......
配置server 80口
server {
listen 80;
server_name localhost;
rewrite ^(.*)$ https://$host$1 permanent;
#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;
}
}
配置443 ssl口
server {
listen 443 ssl;
server_name localhost;
# 在conf目录建立cert目录,拷贝证书文件至此
ssl_certificate cert/5089556_www.yiwiki.cn.pem;
ssl_certificate_key cert/5089556_www.yiwiki.cn.key;
# ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
location / {
# root html;
# index index.html index.htm;
proxy_pass http://yiwiki/;
}
location /status {
check_status;
}
}
停止重启
cd /usr/nginx/sbin
./nginx -s stop
./nginx
建立自启动脚本
这个文件需要修改的是有2个地方,
一个是,
#!/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
#########################上面的pid文件,需要在服务器配置文件/usr/nginx/conf/nginx.conf中,去掉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/nginx/sbin/nginx" prog=$(basename $nginx)
#######################需要修改为实际的配置服务器文件的所在位置 NGINX_CONF_FILE="/usr/nginx/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` if [ -n "$user" ]; then if [ -z "`grep $user /etc/passwd`" ]; then useradd -M -s /bin/nologin $user fi options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done fi } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs 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 sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $prog -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
在自启动脚本里,pid,有一个/var/run/nginx.pid,回到nginx.conf
打开pid注释并且,将目录指向 /var/run/nginx.pid ,需要提前建立目录和pid空文件。然后重启nignx,,ps aux | grep nginx 杀死进程,重启。
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
# 以下为 关键代码,需要改成和nginx启动脚本一致的目录文件
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
chkconfig --add /etc/init.d/nginx
chkconfig nginx on
systemctl daemon-reload
systemctl enable nginx
systemctl start nginx