Nginx学习笔记
文章目录
简单学习下nginx,整理各个分支记忆点
常用命令
启动nginx:
`systemctl start nginx.service`查看nginx进程:
`ps aux | grep nginx`关闭服务:
`nginx -s quit`从容关闭(进程完成任务后再进行关闭)
`nginx -s stop`强制关闭 (屋里进程情况,直接关闭)
`systemctl stop nginx.service`重启服务:
`systemctl restart nginx.service`
重新加载配置文件:
有时我们会修改配置文件,需要nginx重新读取配置
`nginx -s reload`
限制访问
server下的location配置
...
http {
...
server {
...
location / {
allow 100.100.100.100;
deny all;
}
}
}deny: 拒绝访问allow:允许访问
隐藏坑:location,同一个块下的权限配置前面会覆盖后面的
上面例子:允许100.100.100.100访问,拒绝其他ip
如果将deny all;放在上面,就会全部ip都被拒绝
对某些目录精准配置
如:imgs目录对所有的ip都允许访问
location =/imgs {
allow all;
}正则匹配
对所有php结尾的文件禁止访问
location ~\.php$ {
deny all;
}自配置
在nginx.conf中使用include引入自配置项
比如想再配置一个server配置
子配置文件demo.conf
server {
listen 8081;
server_name localhost;
root /usr/share/nginx/html;
index index.html;
}主配置文件nginx.conf
http {
// 其他server配置
server {}
// 因为demo.conf内配置的是server
// 所以需要在server层级进行include
include /etc/nginx/conf.d/demo.conf;
}代理
正向代理:代理客户端,翻墙,vpn
反向代理:代理服务端
server_name demo.test.com
location / {
proxy_pass demo2.test2.com
}例子中,客户端访问的是demo.test.com服务器,真实请求被代理到demo2.test2.com
其他反向代理指令
proxy_set_header 可以设置客户端请求的请求头
proxy_connect_timeout 设置后端与代理服务器建立连接的超时时间
proxy_read_timeout
proxy_send_timeout
proxy_redirect 用于修改后端服务器返回的相应头的Location和Refreshgzip
浏览器支持gzip,在请求头里会自动加入Accept-Encoding: gzip;
nginx开启gzip,在响应头里有Content-Encoding: gzip
http {
gzip on; // 开启gzip
gzip_comp_level 1; // 压缩等级1-9,由高到低,级别越高压缩率越高,越耗时
gzip_disable // 可以设置对于某些user_agent不进行压缩
gzip_min_length // 设置允许压缩的页面的最小字节数,字节数从相应消息头的content-length字段获取
}
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论