Nginx基础篇
安装目录讲解
路径 | 类型 | 作用 |
---|---|---|
/etc/logrotate.d/nginx | 配置文件 | Nginx日志轮转,用于logrotate服务的日志切割 |
/etc/nginx | 目录配置文件 | Nginx主配置文件 |
/etc/nginx/nginx.conf | ||
/etc/nginx/conf.d | ||
/etc/nginx/conf.d/default.conf | ||
/etc/nginx/fastcgi_params | 配置文件 | cgi配置相关fastcgi配置 |
/etc/nginx/uwsgi_params | ||
/etc/nginx/scgi_params | ||
/etc/nginx/mime.types | 配置文件 | 设置http协议的Content-Type与扩展名对应关系 |
/usr/lib/systemd/system/nginx-debug.server | 配置文件 | 用于配置出系统守护进程管理器管理方式 |
/usr/lib/systemd/system/nginx.server | ||
/etc/sysconfig/nginx | ||
/etc/sysconfig/nginx-debug | ||
/usr/lib64/nginx/modules | 目录 | Nginx模块目录 |
/etc/nginx/modules | ||
/usr/sbin/nginx | 命令 | Nginx服务的启动管理的终端命令 |
/usr/sbin/nginx-debug | ||
/var/cache/nginx | 目录 | 缓存目录 |
var/log/nginx | 目录 | 日志目录 |
/etc/nginx/nginx.conf
usr nginx;
worker_process 1;
error_log /var/log/nginx/error.log warn;
pid {
/var/run/nginx.pid;
}
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format '......';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keeplive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
复制代码
/etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 404 /50x.html;
location /50x.html {
root /usr/share/nginx/html;
}
}
复制代码
Nginx官方模块
Nginx的客户状态
编译选项 --with-http_stub_status_module
Syntax:stub_status;
Default:--
Context:server,location;
#default.conf文件下
location /mystatus {
stub_status
}
#检查语法是否正确
nginx -tc /etc/nginx/default.conf
#重启nginx访问IP/mystatus显示Nginx的客户端状态
复制代码
- Active conections : 2
- server accepts handled requests : 396 396 1293
- Reading : 0
- Writing : 1
- Waiting : 1
目录中选择一个随机主页
编译选项 --with-http_random_index_module
Syntax:random_index on|off;
Default:random_index off;
Context:location;
#default.conf文件下
location / {
root /usr/share/nginx/html;#(该目录下有多个html页面)
random_index on;
}
复制代码
HTTP内容替换
编译选项 --with-http_sub_module
Syntax:sub_filter string replacement;
Default:--
Context:http server location
#default.conf文件下
location / {
root /opt/app/code;
idnex index.html index.htm;
sub_filter '<a>imooc' '<a>IMOOC'
}
复制代码
Nginx的请求限制
- 连接频率限制 limit_conn_module
- 请求频率限制 limit_req_module
- HTTP请求建立在一次TCP连接基础上
- 一次TCP请求至少产生一次HTTP请求
limit_conn_zone $binary_remote_addr zone=conn_zone:1m;
limit_req_zone $binary_remote_addr zone=req_zone:1m rate=1r/s;
server {
......
location / {
root /opt/app/code;
limit_conn conn_zone 1;
limit_req conn_req burust=3 nodelay;
index index.html index.htm;
}
......
}
复制代码
Nginx的访问控制
- 基于IP的访问控制 http_access_module
- 基于用户的信任登陆 http_auth_basic_module(访问网站需要账号密码,人数越多,账号密码越多,配置管理麻烦不常见)
Syntax: allow/deny address|CIDR|unix|all
Default:--
Context:http server location limit_except
location ~ ^/admin.html {
root /opt/app/code;
deny 222.128.189.17;
allow all;
idnex index.html index.htm;
}
复制代码
Nginx场景实践篇
静态资源WEB服务
静态资源类型:非服务器动态运行生成的文件
类型 | 种类 |
---|---|
浏览器端渲染 | HTML CSS JS |
图片 | JPEG GIF PNG |
视频 | FLV MPEG |
文件 | TXT等任意下载文件 |
静态资源服务场景-CDN
相关配置
文件读取
Syntax:sendfile on|off;
Default:sendfile off;
Context: http server location
复制代码
sendfile开启的情况下提高网络包的传输效率
Syntax: tcp_nopush on|off;
Default: tcp_nopush off;
Context: http server location
复制代码
keepalive连接下提高网络包的传输实时性
Syntax: tcp_nodelay on|off;
Default: tcp_nodelay on;
Context: http server location
复制代码
压缩传输
Syntax: gzip on|off;
Default: gzip off;
Context: http server location
复制代码
压缩比
Syntax: gzip_comp_level | level;
Default: gzip_comp_level 1;
Context: http server location
复制代码
扩展Nginx压缩模块
http_gzip_static_module -- 预读gzip功能
location ~ .*\.(jpg|gif|png)$ {
gzip on;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javacsript application/x-httpd-php image/jpeg image/gif image/png;
root /opt/app/code/images;
}
location ~ .*\.(text|xml) {
#同上
}
#预读预览压缩文件
location ~ ^/download {
gzip_static on;
tcp_nopush on;
root /opt/app/code;
}
复制代码
浏览器缓存
HTTP协议定义的缓存机制(如:Expires;Cache-Control等)
Syntax: expires [modified] time;
expires epush|max|off;
Default: expires off;
Context: http server location
location ~ .*\.(htm|html)$ {
expires 24h;
root /opt/app/code;
}
复制代码
跨站访问
Syntax: add_header name value [always]
Default: --
Context: http server location
Access-Control-Allow-Origin
location ~ .*\.(htm|html) {
add_header Access-Control-Allow-Origin htp://www.jesonc.com;
add_header Access-Control-Allow-Origin-Methods GET.POST.DELETE.OPTIONS;
root /opt/app/code;
}
复制代码
防盗链
Syntax:valid_refers none|blocked|servernames|string ...;
Default:--
Context: server location
location ~ .*\(jpg|gif|png)$ {
valid_referers none blocked 116.62.103.228;
if ($invalid_referer) {
return 403;
}
root /opt/app/code/images;
}
复制代码
代理服务
- 正向代理:翻墙(为客户端服务)
location / {
if ($http_x_forwarded_for !~"116\.62\.103\.208") {
return 403;
}
root /opt/app/code;
index idnex.htm index.html;
}
复制代码
- 反向代理:负载均衡(为服务端服务)
location ~ /test_proxy.html $ {
proxy_pass http://127.0.0.1:8080;
}
复制代码
负载均衡
看之前的博客
Nginx深度学习篇
动静分离
upstream java_api {
server 127.0.0.1:8080;
}
location ~ \.jsp {
proxy_pass http://java_api;
index index.html index.htm;
}
location ~ \.(jpg|png|gif)$ {
expires 1h;
gzip on;
}
复制代码
rewirte规则
#所有页面都指向一个维护页面
rewirte ^(.*)$ /pages/maintain.html break;
复制代码
Ngixn架构篇
相同server_name多个虚拟主机访问优先级
第一个
location匹配优先级
第一个
try_files的使用
#按顺序检查文件是否存在
location / {
try_files $uri $uri/ /index.php;
}
复制代码
错误代码
- 413 Request Entity Too Large
- 用户上传文件限制 client_max_body_size
- 502 bad gateway
- 后端服务无响应(网关)
- 504 Gateway Time-out
- 后端服务执行超时
性能优化
ab工具的使用
视频地址 https://coding.imooc.com/class/121.html