什么是Nginx?
Nginx是一款收费开源的高性能HTTP服务器
以及反向代理服务器
(Reverse Proxy),同时能够提供IMAP/POP3/SMATP代理服务等性能。可能疾速的响应动态页面申请和反对第三方功能模块扩大。
Nginx的长处
- 高并发、高性能(官网给出的并发数据5万,理论中能够达到2-4万)
- 轻量级、内存耗费少
- 稳定性高,宕机概率低
- 反对热部署
- 模块化设计,扩展性较好
- cpu亲和
Nginx罕用的场景
- 动态资源服务器
- 动静匹配
- 反向代理
- Gzip压缩
- 负载平衡
Nginx的装置配置
mac下镜像飞速装置Homebrew教程: https://zhuanlan.zhihu.com/p/...
$ brew install nginx
Nginx罕用的命令
- 启动:
nginx
- 查看版本号:
nginx -v
- 查看nginx 编译的参数:
nginx -V
- 重新启动nginx:
nginx -s reload
- 优雅重启,并从新载入配置文件nginx.conf:
/usr/local/nginx/sbin/nginx -s reload
优雅进行nginx,有连贯时会等连贯申请实现再杀死worker过程
/usr/local/nginx/sbin/nginx -s quit
具体罕用的命令参考如下:nginx -s stop 疾速敞开Nginx,可能不保留相干信息,并迅速终止web服务。nginx -s quit 安稳敞开Nginx,保留相干信息,有安顿的完结web服务。nginx -s reload 因扭转了Nginx相干配置,须要从新加载配置而重载。nginx -s reopen 从新关上日志文件。nginx -c filename 为 Nginx 指定一个配置文件,来代替缺省的。nginx -t 不运行,仅测试配置文件。nginx 将查看配置文件的语法的正确性,并尝试关上配置文件中所援用到的文件。nginx -v 显示 nginx 的版本。nginx -V 显示 nginx 的版本,编译器版本和配置参数。
胜利看到欢送页面~!(对应的html是/usr/local/var/www/index.html)
Nginx的默认配置
Nginx 装置目录下的nginx.conf就是Nginx全局的配置文件,咱们次要批改这里的内容。nginx.conf.default作为配置文件的备份。
nginx默认应用8080端口 如果发现端口被占用了,能够杀掉应用应用改端口的过程,也能够批改/usr/local/etc/nginx/nginx.conf
下的
http { server { listen 8181; server_name localhost; #charset koi8-r; ..... } }
其中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寄存地位#pid logs/nginx.pid;# 解决连贯events { # 设置连接数,单个后盾过程的最大并发数 worker_connections 1024;}http { # 文件拓展名查找汇合 include mime.types; # 当查找不到对应类型的时候默认值 default_type application/octet-stream; # 日志格局,定义别名为 main #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #nginx拜访日志寄存地位 #access_log logs/access.log main; # 调用 sendfile 零碎传输文件 sendfile on; #开启高效传输模式 #tcp_nopush on; #缩小网络报文段的数量 # 客户端与服务器连贯超时工夫,超时主动断开 #keepalive_timeout 0; keepalive_timeout 65; # 开启gizip 压缩 #gzip on; # 虚拟主机 server { listen 8181; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; # 路由 location / { root html; index index.html index.htm; #第一种状况 回绝拜访ip地址段为 50-100 的ip拜访 deny 192.168.10.50/100; # 第二种状况 只容许ip地址为 192.168.10.50 的ip拜访 allow 192.168.10.50; deny all; # 第三种状况 这样配置都不能拜访,从上到下顺次匹配 deny all; allow 192.168.10.50; } #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; # } #} include servers/*;}
搭建动态站点
# 虚拟主机server块server { # 端口 listen 8080; # 匹配申请中的host值 server_name localhost; # 监听申请门路 location / { # 查找目录 root /source; # 默认查找 index index.html index.htm; }}
字段阐明:
- server 配置虚拟主机的相干参数,能够有多个
- server_name 通过申请中的host值 找到对应的虚拟主机的配置
- location 配置申请路由,解决相干页面状况
- root 查找资源的门路
配置实现后执行 nginx -t
看是否有谬误,如果看到的是上面这种就是胜利了
nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful
而后执行nginx -s reload
更新Nginx配置文件,这时候关上浏览器 输出 localhost:8080 应该就能看到你的页面了。
动静匹配(申请过滤)
通常在开发环境或者测试环境的时候呢咱们批改了代码,因为浏览器缓存,可能不会失效,须要手动革除缓存,能力看到批改后的成果,这里咱们做一个配置让浏览器不缓存相干的资源。
location ~* \.(js|css|png|jpg|gif)$ { add_header Cache-Control no-store;}
~* .(js|css|png|jpg|gif)$
是匹配以相干文件类型而后独自解决。 add_header
是给申请的响应加上一个头信息Cache-Control no-store
,告知浏览器禁用缓存,每次都从服务器获取 成果如下:
匹配规定
location = / { [ configuration A ]}location / { [ configuration B ]}location /documents/ { [ configuration C ]}location ^~ /images/ { [ configuration D ]}location ~* \.(gif|jpg|jpeg)$ { [ configuration E ]}
location =|~|~*|^~| /uri/
- = 示意准确匹配。只有申请的url门路与前面的字符串齐全相等时,才会命中(优先级最高)。
- ^~ 示意如果该符号前面的字符是最佳匹配,采纳该规定,不再进行后续的查找。
- ~ 示意该规定是应用正则定义的,辨别大小写。
- ~* 示意该规定是应用正则定义的,不辨别大小写。
/ 通用匹配,任何申请都会匹配到
通过状态码来过滤申请
# 通过状态码,返回指定的谬误页面error_page 500 502 503 504 /50x.html;location = /50x.html { root /source/error_page;}
反向代理解决跨域
在前后端拆散的开发中,跨域问题是一个十分常见的问题,当初解决跨域问题比拟罕用的两种形式为:
- 跨域资源申请(CORS)
- Nginx反应代理
先来看下面的图 ,当用户申请xx.720ui.com/server1的时候,Nginx会将申请转发给Server1这个服务器上的具体利用,从而达到跨域的目标
同时nginx解决跨域时罕用的参数配置。
location /api { # 申请host传给后端 proxy_set_header Host $http_host; # 申请ip 传给后端 proxy_set_header X-Real-IP $remote_addr; # 申请协定传给后端 proxy_set_header X-Scheme $scheme; # 门路重写 rewrite /api/(.*) /$1 break; # 代理服务器 proxy_pass http://localhost:9000;}
- 拦挡门路/api, 能够通过正则匹配
- proxy_set_header 容许从新定义或增加字段传递给代理服务器的申请头
$http_host
$remote_addr、$scheme 为Nginx内置变量- rewrite 依据rewrite后的申请URI,将门路重写,如:接口门路为 /user, 咱们能够申请 /api/user。(为什么须要重写uri?因为在应用Nginx做反向代理的时候,须要匹配到跨域的接口再做转发,为了不便匹配,会人为的在原接口中增加一段门路(或标示, 如例子中的api),因而须要在匹配之后、转发之前把增加的那段去掉,因而须要rewrite)
- break 持续本次申请前面的解决 ,进行匹配上面的location。须要留神的是与之类似的last执行过程则是进行以后这个申请,并依据rewrite匹配的规定从新发动一个申请,从上到下顺次匹配location前面的规定
proxy_pass 代理服务器
原理:Nginx拦挡到相干匹配规定, Nginx再将申请转发到http://localhost:9090,Nginx...
nginx跨域申请一个简略的dome:server{ listen 80; server_name www.1212.com; location ^~ /blog/ { proxy_pass http://blog.12121.com/; } }
总结:功败垂成✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️
参考链接:
- https://juejin.cn/post/684490...
- https://juejin.cn/post/684490...
- https://juejin.cn/post/684490...