技术代码:Nginx

11、先用新程序替换旧程序文件
2
3  2、kill -USR2 旧版程序的主进程号或者进程文件名
4
5    此时旧的nginx主进程会把自己的进程文件改名为.oldbin,然后执行新版nginx,此时新旧版本同时运行
6
7  3、kill -WINCH 旧版本主进程号
8
9  4、不重载配置启动新/旧工作进程
10
11    kill -HUP/新版本主进程号
12
13    从容关闭旧/新进程
14
15    kill -QUIT/新进程号
16
17    快速关闭旧/新进程
18
19    kill -TERM/新进程号
参考地址:Nginx相关命令合集
1//进入/usr/local目录
2cd /usr/local
3//创建一个文件夹nginx
4mkdir nginx
5//进入nginx文件夹
6cd nginx
7//下载tar包
8wget http://nginx.org/download/nginx-1.13.7.tar.gz
9//解压tar包
10tar -xvf nginx-1.13.7.tar.g
参考地址:Linux下安装nginx
1worker_processes  1;
2
3
4events {
5    #允许的连接数
6    worker_connections  1024;
7}
8
9
10http {
11	
12    include       mime.types;
13    default_type  application/octet-stream;
14
15
16    sendfile        on;
17
18    keepalive_timeout  65;
19	
20
21    server {
22        listen       80;
23        server_name  localhost;
24	location / {
25	    # 代理静态页面
26	    root     E:/rjhc/block_chain_management/dist;
27	}
28		
29        error_page   500 502 503 504  /50x.html;
30        location = /50x.html {
31            root   html;
32        }
33        # 代理接口
34	location /v1/ {
35	    proxy_set_header Host $host;
36	    proxy_set_header X-Real-IP $remote_addr;
37	    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
38	    proxy_pass http://127.0.0.1:8080/;
39	}
40    }
41
42}
参考地址:nginx window
1[Unit]
2Description=nginx 
3After=network.target 
4   
5[Service] 
6Type=forking 
7ExecStart=/usr/local/nginx/sbin/nginx
8ExecReload=/usr/local/nginx/sbin/nginx reload
9ExecStop=/usr/local/nginx/sbin/nginx quit
10PrivateTmp=true 
11   
12[Install] 
13WantedBy=multi-user.target
参考地址:linux CentOS 安装 nginx
1D:\nginx\nginx-1.12.0>nginx -h
2nginx version: nginx/1.12.0
3Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
4
5Options:
6  -?,-h         : this help
7  -v            : show version and exit
8  -V            : show version and configure options then exit
9  -t            : test configuration and exit
10  -T            : test configuration, dump it and exit
11  -q            : suppress non-error messages during configuration testing
12  -s signal     : send signal to a master process: stop, quit, reopen, reload
13  -p prefix     : set prefix path (default: NONE)
14  -c filename   : set configuration file (default: conf/nginx.conf)
15  -g directives : set global directives out of configuration file
参考地址:Nginx启动、关闭与重启
1#user  nobody;
2worker_processes  4; //一般为当前机器总cpu核心数的1到2倍。
3
4#error_log  logs/error.log;
5#error_log  logs/error.log  notice;
6#error_log  logs/error.log  info;
7
8#pid        logs/nginx.pid;
9
10
11events {
12    worker_connections  1024;
13}
14
15http {
16    include       mime.types;
17    default_type  application/octet-stream;
18sendfile        on;
19    #tcp_nopush     on;
20
21    #keepalive_timeout  0;
22    keepalive_timeout  65;
23
24    #gzip  on;
25    upstream node_app {
26        server 127.0.0.1:3000; //根据所开的nodejs端口
27    }
28
29    server {
30        listen       80;
31        server_name  localhost;
32
33        #charset koi8-r;
34
35        #access_log  logs/host.access.log  main;
36
37        location / {
38            proxy_pass http://node_app;
39        }
40    }
41}
参考地址:nodejs+express+nginx
1upstream www.wu.com{
2	server localhost:8090 weight=3;
3	server localhost:9090 weight=1;
4    }
5
6    server {
7        listen       80;
8        #server_name  localhost;
9	server_name  www.wuwh.com;
10        
11        location / { 
12        #proxy_pass         http://www.wuwh.com ;
13	proxy_pass         http://www.wu.com ;
14        proxy_set_header   Host             $host; 
15        proxy_set_header   X-Real-IP        $remote_addr; 
16        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for; 
17    }
参考地址:win7系统下安装、配置、测试nginx
1#user  nobody;
2worker_processes  1;
3
4error_log  logs/error.log;
5#error_log  logs/error.log  notice;
6#error_log  logs/error.log  info;
7
8#pid        logs/nginx.pid;
9
10
11events {
12    worker_connections  1024;
13}
14
15
16http {
17    include       mime.types;
18    default_type  application/octet-stream;
19
20    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
21    #                  '$status $body_bytes_sent "$http_referer" '
22    #                  '"$http_user_agent" "$http_x_forwarded_for"';
23
24    #access_log  logs/access.log  main;
25
26    sendfile        on;
27    #tcp_nopush     on;
28
29    #keepalive_timeout  0;
30    keepalive_timeout  65;
31
32    #gzip  on;
33
34	server {
35		listen          80;
36		server_name     abc.abc.com; # 或者 localhost
37
38		location / {
39			
40            tcp_nodelay     on;
41            proxy_set_header Host            $host;
42            proxy_set_header X-Real-IP       $remote_addr;
43            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
44            #以上三行,目的是将代理服务器收到的用户的信息传到真实服务器上
45		
46            root   /home/html;
47            index  index.html index.htm;
48         
49         	# 请求转发到89端口
50			#proxy_pass http://127.0.0.1:89;
51		}
52	}
53 
54
55
56    # HTTPS server
57    
58    server {
59           
60		listen 443 ssl;   #SSL协议访问端口号为443。此处如未添加ssl,可能会造成Nginx无法启动。
61		server_name abc.abc.com;  #将localhost修改为您证书绑定的域名,例如:www.example.com。
62		root html;
63		index index.html index.htm;
64		ssl_certificate xxx.pem;   #将domain name.pem替换成您证书的文件名。
65		ssl_certificate_key xxx.key;   #将domain name.key替换成您证书的密钥文件名。
66		ssl_session_timeout 5m;
67		ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;  #使用此加密套件。
68		ssl_protocols TLSv1 TLSv1.1 TLSv1.2;   #使用该协议进行配置。
69		ssl_prefer_server_ciphers on;   
70		location / {
71			root html;   #站点目录。
72			index index.html index.htm;   
73		}
74		
75		# 关键参数:这个变量开启后,我们才能自定义错误页面,当后端返回404,nginx拦截错误定义错误页面
76		proxy_intercept_errors on;
77		#开启error_page
78		error_page  404          /404.html;
79
80        error_page   500 502 503 504  /50x.html;
81    }
82}
参考地址:nginx 常用命令及config配置
1#!/bin/bash  
2# nginx Startup script for the Nginx HTTP Server  
3# it is v.0.0.2 version.  
4# chkconfig: - 85 15  
5# description: Nginx is a high-performance web and proxy server.  
6#              It has a lot of features, but it's not for everyone.  
7# processname: nginx  
8# pidfile: /var/run/nginx.pid  
9# config: /usr/local/nginx/conf/nginx.conf  
10nginxd=/usr/local/nginx/sbin/nginx  
11nginx_config=/usr/local/nginx/conf/nginx.conf  
12nginx_pid=/var/run/nginx.pid  
13RETVAL=0  
14prog="nginx"  
15Source function library.  
16. /etc/rc.d/init.d/functions  
17Source networking configuration.  
18. /etc/sysconfig/network  
19Check that networking is up.  
20[ ${NETWORKING} = "no" ] && exit 0  
21[ -x $nginxd ] || exit 0  
22Start nginx daemons functions.  
23start() {  
24if [ -e $nginx_pid ];then  
25   echo "nginx already running...."  
26   exit 1  
27fi  
28   echo -n $"Starting $prog: "  
29   daemon $nginxd -c ${nginx_config}  
30   RETVAL=$?  
31   echo  
32   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx  
33   return $RETVAL  
34}  
35Stop nginx daemons functions.  
36stop() {  
37        echo -n $"Stopping $prog: "  
38        killproc $nginxd  
39        RETVAL=$?  
40        echo  
41        [ $RETVAL = 0 ] && rm -/var/lock/subsys/nginx /var/run/nginx.pid  
42}  
43# reload nginx service functions.  
44reload() {  
45    echo -n $"Reloading $prog: "  
46    #kill -HUP `cat ${nginx_pid}`  
47    killproc $nginxd -HUP  
48    RETVAL=$?  
49    echo  
50}  
51See how we were called.  
52case "$1" in  
53start)  
54        start  
55        ;;  
56stop)  
57        stop  
58        ;;  
59reload)  
60        reload  
61        ;;  
62restart)  
63        stop  
64        start  
65        ;;  
66status)  
67        status $prog  
68        RETVAL=$?  
69        ;;  
70*)  
71        echo $"Usage: $prog {start|stop|restart|reload|status|help}"  
72        exit 1  
73esac  
74exit $RETVAL
参考地址:CentOS 6.5 安装 Nginx
11.编写shell脚本
2[root@localhost /]#vi /etc/init.d/nginx  (输入下面的代码)
3
4#!/bin/bash
5# nginx Startup script for the Nginx HTTP Server
6# it is v.0.0.2 version.
7# chkconfig: - 85 15
8# description: Nginx is a high-performance web and proxy server.
9#              It has a lot of features, but it's not for everyone.
10# processname: nginx
11# pidfile: /var/run/nginx.pid
12# config: /usr/local/nginx/conf/nginx.conf
13nginxd=/usr/local/nginx/sbin/nginx
14nginx_config=/usr/local/nginx/conf/nginx.conf
15nginx_pid=/var/run/nginx.pid
16RETVAL=0
17prog="nginx"
18# Source function library.
19. /etc/rc.d/init.d/functions
20# Source networking configuration.
21. /etc/sysconfig/network
22# Check that networking is up.
23[ ${NETWORKING} = "no" ] && exit 0
24[ -x $nginxd ] || exit 0
25# Start nginx daemons functions.
26start() {
27if [ -e $nginx_pid ];then
28   echo "nginx already running...."
29   exit 1
30fi
31   echo -n $"Starting $prog: "
32   daemon $nginxd -c ${nginx_config}
33   RETVAL=$?
34   echo
35   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
36   return $RETVAL
37}
38# Stop nginx daemons functions.
39stop() {
40        echo -n $"Stopping $prog: "
41        killproc $nginxd
42        RETVAL=$?
43        echo
44        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
45}
46# reload nginx service functions.
47reload() {
48    echo -n $"Reloading $prog: "
49    #kill -HUP `cat ${nginx_pid}`
50    killproc $nginxd -HUP
51    RETVAL=$?
52    echo
53}
54# See how we were called.
55case "$1" in
56start)
57        start
58        ;;
59stop)
60        stop
61        ;;
62reload)
63        reload
64        ;;
65restart)
66        stop
67        start
68        ;;
69status)
70        status $prog
71        RETVAL=$?
72        ;;
73*)
74        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
75        exit 1
76esac
77exit $RETVAL
78
79:wq  保存并退出
80
812.设置文件的访问权限
82[root@localhost init.d]# chmod a+x nginx (a+x ==> all user can execute  所有用户可执行)
83这样在控制台就很容易的操作nginx了:查看Nginx当前状态、启动Nginx、停止Nginx、重启Nginx…
参考地址:【Nginx】的快速安装

代码交流 2021