一、Nginx安装和代理转发到应用服务

  1. Nginx入门学习笔记
    Windows系统:
    1、到nginx官网下载,解压即可使用(解压到没有汉字的目录下)
    2、修改nginx.conf文件中http节点下server的端口号,可以配置多个server节点端口
    3、使用upstream关键字创建负载均衡配置(weight权重负载均衡配置)
    4、常用nginx操作命令,进入到nginx项目根目录下:
    启动:nginx.exe
    重新加载配置文件:nginx -s reload
    快速停止nginx:nginx -s stop
    完整有序的停止nginx:nginx -s quit

        Linux系统:
        安装

        1】下载
        wget http://nginx.org/download/nginx-1.21.3.tar.gz

        2】解压
        tar -zxvf nginx-1.21.3.tar.gz

        3】解压后,进入到 nginx-1.21.3 根目录下:

        4】编译和安装,执行如下命令:
              默认安装目录:
              1、编译命令:./configure
              2、安装命令:make && make install
              说明:默认安装目录为:/usr/local/nginx

              指定安装目录:
              执行如下命令:
              ./configure --prefix=/hkl/devtools/nginx --sbin-path=/hkl/devtools/nginx/sbin/nginx --conf-path=/hkl/devtools/nginx/conf/nginx.conf --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module && make && make install
        含义解释:
        --prefix:nginx的安装目录
        --sbin-path:nginx的启动程序目录
        --conf-path:指定以xxx.conf配置信息启动nginx
        --with-xxx:nginx需要加载的模块,如果需要nginx支持https,则需要加入http_ssl_module模块

        以上配置参数可根据自身实际情况相应调整

        常用命令
        1、测试nginx配置文件是否正确:nginx -t
        2、启动:nginx
        3、指定配置文件启动:nginx -c xxx/xxx/nginx.conf
        4、重启nginx服务:sudo service nginx restart
        5、重新加载配置文件:nginx -s reload
        6、停止:nginx -s stop 或者 nginx -s quit
        7、查看nginx进程:ps -ef|grep nginx
        8、查找nginx安装路径:whereis nginx

2、nginx.conf文件配置说明和代码如下:

#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;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
	
	
	#负载均衡资源配置,(服务器集群)
	upstream demoproject {
		server	127.0.0.1:8001	weight=1;
		server	127.0.0.1:8002	weight=1;
	}

	#服务配置(可配置多个server节点)
    server {
		#端口号
        listen       6060;
		#服务名称
        server_name  localhost;

		#代理配置
        location / {
            root   html;
            index  index.html index.htm;
			#代理到集群资源(upstream)
			proxy_pass	http://demoproject;
        }
		
		error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
	
	
	
	
	
	
	#负载均衡资源配置,(服务器集群)
	upstream demoproject6070 {
		server	127.0.0.1:8003	weight=1;
		#server	127.0.0.1:8002	weight=1;
	}

	#服务配置(可配置多个server节点)
    server {
		#端口号
        listen       6070;
		#服务名称
        server_name  localhost;

		#代理配置
        location / {
            root   html;
            index  index.html index.htm;
			#代理到集群资源(upstream)
			proxy_pass	http://demoproject6070;
        }
		
		error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

}

3、Nginx常用负载均衡策略: 
     1】轮询:默认策略
     2】weight权重策略,配置如下:

upstream demoproject {
                server 127.0.0.1:8001 weight=2;
                server 127.0.0.1:8002 weight=1;
        }

        说明:weight的值越大,被发送请求的频率就越高

     3】backup备用策略,配置如下:
              server localhost:8082 backup;
              说明:标记该服务器为备用服务器。当主服务器停止时,请求会被发送到它这里

     4】least_conn较少链接策略,配置如下:
             upstream demoproject {
                        least_conn; #把请求转发给连接数较少的后端服务器
                        server 127.0.0.1:8001 weight=2;
                        server 127.0.0.1:8002 weight=1;
                }
                说明:此负载均衡策略适合请求处理时间长短不一造成服务器过载的情况。

二、Nginx代理转发到静态资源文件
1、nginx.conf文件在server节点下配置如下代码:

location /xxx {
    alias   D:/DevToolsInstall/nginx/nginx-1.21.0/app/dist;
	index  index.html index.htm;
}

说明:
1】xxx:访问上下文的根目录,类似于springboot项目yml中配置tomcat servlet的上下文根目录
2】alias:静态资源的全路径,注意windows转发路径是/左斜杠
3】index:请求的默认访问页面