Nginx

系列文章目录

一、什么是正向代理?

如果客户端请求目标服务器之间的一个代理服务器,这个请求回先经过代理服务器,然后再转发请求到目标服务器,获得内容之后响应给客户端。
在这里插入图片描述

二、什么是反向代理?

用户请求目标服务器,由代理服务器决定转发到哪个IP。
就好像老师该到哪个教室上课是由教务处安排的。
在这里插入图片描述

三、反向代理-路由

首先Nginx通过域名可以构建虚拟主机,根据虚拟主机的规则去访问特定的web服务器或可以根据配置的路由去访问特定路径下的静态资源。
在这里插入图片描述

四、请求Nginx默认页面

客户端在浏览器输入需要访问的目标服务器Nginx,这时Nginx会有一个对应的server监听到对应的端口和域名,如果域名和端口号对应的情况下,Nginx会把请求地址映射到配置的地址。
在这里插入图片描述

五、Nginx进程模型

在这里插入图片描述

1.master进程(主进程)

master主进程收到下面这些指令的时候会把相应的操作分发给对应的worker进程
在这里插入图片描述

2.worker进程(工作进程)

六、nginx.conf配置结构

在这里插入图片描述

# worker进程是由哪个用户操作执行的
#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 {
	#Linux系统默认使用epoll,不同系统使用不同
	use epoll
	#每个woker默认允许的最大连接数
    worker_connections  1024;
}
#http指令块,针对http网络传输的网络配置
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使用高效文件传输,提升传输性能。启用后才能使用tcp_nopush,是指当数据表累积一定大小后才发送,提高了效率
    sendfile        on;
    tcp_nopush     on;
	#设置客户端与服务端请求的超时时间,保证客户端多次请求的时候不会重复建立新的连接,节约资源损耗
    #keepalive_timeout  0;
    keepalive_timeout  65;
	#传输资源是否压缩,开启可以提升传输速度,消耗CPU资源
    #gzip  on;
	#虚拟主机,可以配置多个
    server {
    	#监听端口
        listen       80;
        #监听域名
        server_name  localhost;
        #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;
        }
        # 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;
        #}
    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    # HTTPS server
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}
drwxr-xr-x.  2 root root 4096 Apr 11  2018 sbin
▽
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

七、开启压缩

#开启gzip压缩功能,目的:提高传输效率,节约带宽
gzip on;
#限制最小压缩,小于1字节文件不会压缩
gzip_min_length 1;
定义压缩的级别(压缩比,文件越大,压缩越多,但是cpu使用会越多)
gzip_comp_level 3;
#定义压缩文件的类型
gzip_types text/plain application/javascript application/x-javascript text/css application /xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/json;

八、location 的匹配规则

1、空格 :默认匹配,普通匹配

location / { 
	root /home; 
	}

2、= :精确匹配

location = /imooc/img/face1.png { 
	root /home;
 }

3、~* :匹配正则表达式,不区分大小写

#符合图片的显示 
location ~ *\.(GIF|jpg|png|jpeg) { 
	root /home;
}

4、~ :匹配正则表达式,区分大小写

#GIF必须大写才能匹配到 
location ~ \.(GIF|jpg|png|jpeg) {
 	root /home; 
 }

5、^~ :以某个字符路径开头

location ^~ /imooc/img { 
	root /home;
}

九、跨域配置

在虚拟主机server中配置

#允许跨域请求的域,*代表所有
add_header 'Access-Control-Allow-Origin' *; 
#允许带上cookie请求
add_header 'Access-Control-Allow-Credentials' 'true';
#允许请求的方法,比如 GET/POST/PUT/DELETE 
add_header 'Access-Control-Allow-Methods' *; 
#允许请求的header
add_header 'Access-Control-Allow-Headers' *; 

十、配置静态资源防盗链

#对源站点验证
valid_referers*.imooc.com; 
# 非法引入会进入下方判断
if ($invalid_referer){ 
	return 404;
{ 

十一、缓存

1、浏览器的缓存

加速用户访问,提升单个用户(浏览器的访问者)的体验

2、Nginx缓存

缓存在nginx端,提升所有访问到nginx这一端的用户
提升访问上游(upstream)服务器的速度
用户访问仍然会产生请求流量

在用户请求过程中,上游服务器资源可以缓存到Nginx中,然后Nginx中的资源可以被缓存到浏览器中,这样可以大大减少用户的请求响应时间。

在这里插入图片描述

2.1 通过expires控制浏览器缓存

在localtion模块中配置expires指令可以控制浏览器中的缓存
下面缓存的作用分别是

  1. 多少时间后缓存刷新
  2. 在什么时间进行缓存刷新
  3. 在当前时间的先前多少时间缓存就失效
  4. 1970年缓存过期,缓存没有存到浏览器中
  5. 关闭Nginx控制缓存,默认使用浏览器的缓存
    在这里插入图片描述

3、Nginx的反向代理缓存

反向代理访问的web服务器的资源被缓存在Nginx中。

#proxy_cache_path 设置缓存保存的目录
#keys_zone 设置共享内以及占用的空间大小
#max_size设置缓存大小
#inactive 超过此时间,则缓存自动清理
#use_temp_path 关闭临时目录
proxy_cache_path /usr/local/nginx/upsteam_cache keys_zone=mycache:5m max_size=1g ina ctive=30s use_temp_path=off;

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值