nginx看这一遍就够啦

[亡魂溺海] 提交于 2019-11-27 19:13:31

什么是nginx?

nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。由俄罗斯的程序设计师Igor Sysoev所开发,官方测试nginx能够支支撑5万并发链接,并且cpu、内存等资源消耗却非常低,运行非常稳定,所以现在很多知名的公司都在使用nginx。

nginx应用场景

1、http服务器。Nginx是一个http服务可以独立提供http服务。可以做网页静态服务器。
2、虚拟主机。可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。
3、反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。

Windows环境下安装Nginx

软件下载链接
链接:https://pan.baidu.com/s/1XN3oVTXXiUMFgwqUrRoilg
提取码:b6cy
复制这段内容后打开百度网盘手机App,操作更方便哦
解压:nginx-windows
双击: nginx.exe
打开浏览器输出localhost
能看到以下界面说明安装成功

在这里插入图片描述

nginx优缺点

占内存小,可以实现高并发连接、处理响应快。
可以实现http服务器、虚拟主机、反向代理、负载均衡。
nginx配置简单
可以不暴露真实服务器IP地址

nginx实现反向代理

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;

    server {
        listen       80; //监听端口号
        server_name  localhost; //这两个在一起的意思是,只要是通过localhost:80访问的,我统一进行拦截到location具体地址

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass: https://www.baidu.com; //跳转的地址.注意这里最后又个;号
            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;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

什么是负载均衡

负载均衡 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。
负载均衡,英文名称为Load Balance,其意思就是分摊到多个操作单元上进行执行,例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务。
在这里插入图片描述

负载均衡策略

1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
upstream backserver {
server 192.168.0.14;
server 192.168.0.15;
}
2、指定权重
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
upstream backserver {
server 192.168.0.14 weight=10;
server 192.168.0.15 weight=10;
}
3、IP绑定 ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
upstream backserver {
ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}

nginx实现负载均衡

upstream 是配置负载均衡服务器的地址;
实现访问localhost:80进行负载均衡访问8080,和8081服务器

upstream backserver { 
     server 127.0.0.1:8080; 
     server 127.0.0.1:8081; 
    } 

    server {
        listen       80;
        server_name localhost;
        location / {
		    proxy_pass  http://backserver; //这个地址要和updatestream定义名字一样
			index  index.html index.htm;
        }
    }

因为nginx默认是轮训的负载均衡策略,进行访问的时候,每次访问的时候,依次访问的是8080->8081-8080->… 的服务器

nginx实现指定权重

指定权重的优点:能者多劳

upstream backserver { 
     server 127.0.0.1:8080 weight=1; 
     server 127.0.0.1:8081 weight=3;
    } 

    server {
        listen       80;
        server_name  www.itmayiedu.com;
        location / {
		    proxy_pass  http://backserver;
			index  index.html index.htm;
        }
    }

相当于每次访问nginx 8080和8081的访问比例是1比3的

nginx的宕机容错

前面竟然我们配置了2个服务器,那么可能出现一种情况,就是当我们访问8080服务器的时候,此时服务器是宕机的,但是我们为了能够实现的服务器的可用性,我们此时可以让他访问8081去代替工作
就好比,架构师今天生病啦,结果所有的项目问题,全部都交你负责啦

 upstream backserver { 
         server 127.0.0.1:8080 weight=1; 
         server 127.0.0.1:8081 weight=3;
        } 
    
        server {
            listen       80;
            server_name  www.itmayiedu.com;
            location / {
    		    proxy_pass  http://backserver;
    			index  index.html index.htm;
    			proxy_connect_timeout 1; //连接的超时时间
    			proxy_send_timeout 1; //发送的超时时间
    			proxy_read_timeout 1; // 读取的超时时间
            }
        }

此时在进行访问的时候,就会出现无论怎么访问,访问的服务器都是8081服务器

nginx解决跨域问题

什么是跨域问题
在公司工作的时候,我听见隔壁面试官问过这个问题,但是对面好像还不是很明白跨域的问题,反而让面试官给他解释了一波.先不说问题难易,光是这样结果可想而知;
简单的跨域:简单的说就是页面a想获取b页面资源,如果a、b页面的协议、域名、端口、子域名不同,所进行的访问行动都是跨域的,而浏览器为了安全问题一般都限制了跨域访问,也就是不允许跨域请求资源。注意:跨域限制访问,其实是浏览器的限制。理解这一点很重要!!!
比如现在这两个网址 http://a.a.com:81/A 和 http://b.b.com:81/B 他们符合上面跨域的问题 — 如果a、b页面的协议、域名、端口、子域名不同,那么如何使用nginx解决跨域呢?

比如一个请求是是 www.baidu.com/A 和 www.baidu.com/B

server {
        listen       80;
        server_name  www.baidu.com;
        location /A {  //注意这里有个/a
		    proxy_pass  http://a.a.com:81/A;
			index  index.html index.htm;
        }
		location /B {
		    proxy_pass  http://b.b.com:81/B;
			index  index.html index.htm;
        }
    }

在这里www.baidu.com/A可以访问 http://a.a.com:81/A;
www.baidu.com/B可以访问http://b.b.com:81/B;
如何让www.baidu.com/A访问http://b.b.com:81/B;呢?

答案很简单虽然http://a.a.com:81/A; 和 http://b.b.com:81/B; 存在跨域 但是 www.baidu.com/A 和 www.baidu.com/B 不存在
我 www.baidu.com/A 访问的时候,可以直接请求www.baidu.com/B 从而实现跨域问题的解决办法
在这里插入图片描述

nginx 防盗链

** 防盗链** 为了保护网站的资源不被盗取, 比如网站的视频 和 图片,一般的解决方法是利用浏览器的F12 直接找出链接 进行盗用,如何解决这个问题就是防盗链的要做的

比如我在的项目里一个图片用标签 < img href=“wwww.baidu.com/iamg.jpg”> 可以直接实现盗用

我们在nginx只需要配置这些就可以

location ~ .*\.(jpg|jpeg|JPG|png|gif|icon)$ { //主要就是当进访问这些后缀的时候世界给他们返回403
        valid_referers blocked http://www.baidu.com www.baidu.com;
        if ($invalid_referer) {
            return 403;
        }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!