Nginx学习

Nginx学习

静态资源服务器

server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }

listen: 监听的端口号。
server_name: 域名。
location: url匹配,/表示全部匹。
root: 匹配成功之后进入的目录。
index: 默认的页面。

反向代理

    server {
		#监听80端口
        listen       80;
		#浏览器输入的地址
        server_name  localhost;
		#匹配所有的路径
        location / {
			proxy_pass  http://127.0.0.1:8080;
			#表示默认的页面。
            index  index.html index.htm;
        }

proxy_pass: 127.0.0.1可以换成任何一个通的内网地址,这个ip表示你要真实访问的tomcat所在的位置,proxy_pass的值就表示你真正访问的域名是什么。

负载均衡

三种基本的负载均衡算法: 轮询、权重、ip绑定。

轮询策略

upstream  test {
   server 127.0.0.1:8081;
   server 127.0.0.1:8082;
}
server {
	#监听80端口
    listen       80;
	#浏览器输入的地址
    server_name  localhost;
	#匹配所有的路径
    location / {
		proxy_pass  http://test;
		#表示默认的页面。
        index  index.html index.htm;
    }

权重策略

upstream  test {
   server 127.0.0.1:8081 weight=5;
   server 127.0.0.1:8082 weight=1;
}
server {
	#监听80端口
    listen       80;
	#浏览器输入的地址
    server_name  localhost;
	#匹配所有的路径
    location / {
		proxy_pass  http://test;
		#表示默认的页面。
        index  index.html index.htm;
    }

hash策略

upstream  test {
   server 127.0.0.1:8081;
   server 127.0.0.1:8082;
   ip_hash;
}
server {
	#监听80端口
    listen       80;
	#浏览器输入的地址
    server_name  localhost;
	#匹配所有的路径
    location / {
		proxy_pass  http://test;
		#表示默认的页面。
        index  index.html index.htm;
    }
posted @ 2021-03-04 15:23  星时代曹波涛  阅读(51)  评论(0编辑  收藏  举报