nginx入门--根据官方文档整理

新手实用教程

配置文件路径

By default, the configuration file is named nginx.conf and placed in the directory /usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx.

启动/停止命令

nginx -s signal
signal代表以下几项:
stop — 快速关闭
quit — 正常关闭,当前工作进程停止对请求提供服务
reload — 重载配置文件
nginx -s reload
重载检查当前新配置文件并应用配置文件,成功则启动新进程并发送信息使得旧进程关闭,否则主进程回滚并使用旧配置继续工作。
Old worker processes, receiving a command to shut down, stop accepting new connections and continue to service current requests until all such requests are serviced. After that, the old worker processes exit.

reopen — 重开日志文件

kill -s QUIT 1628 — 直接发送给进程的信号正常关闭
对于直接发送给进程的信号,拥有一个给定的进程ID,写在 /usr/local/nginx/logs 或者 /var/run中

常用命令总结

比较 return, rewrite, 和 try_files指令

  • return指令:
    如果可以使用return, 不建议使用更复杂的rewritereturnserverlocation 的上下文中使用,下面是一个简单的例子:
server {
    listen 80;
    listen 443 ssl;
    server_name www.old-name.com;
    return 301 $scheme://www.new-name.com$request_uri;
    // $scheme 代表 protocols http/https
    // $request_uri 是包含参数的完整URI
}
  • rewrite 指令:rewrite regex URL [flag]
  1. 用于测试URLs之间更复杂的区别;
  2. 捕获在nginx变量中没有映射关系的元素
  3. 在路径中改变或添加元素;
    rewrite 指令只能返回code 301 302,如果想要返回其他code,需要在后面添加return指令;
server {
    rewrite ^(/download/.*)/media/(\w+)\.?.*$ $1/mp3/$2.mp3 last;
    rewrite ^(/download/.*)/audio/(\w+)\.?.*$ $1/mp3/$2.ra  last;
    return  403;
}
  • try_files: try_files directories files uri
    nginx 顺次检查这些路径和文件的存在,并服务于第一个匹配的路径/文件;
    服务端请求 http://www.baidu.com/img/img.gif,
    nginx查找顺序:
  1. root 或者 alias指令下的本地路径下的 img.gif;
  2. 查找img/ 目录下的 img.gif
  3. 重定向到 /img/default.gif
location /img/ {
// 在root or alias指令
    try_files $uri $uri/ /img/default.gif;
}
// 在路径不存在时提供的默认GIF
location = /img/default.gif {
    expires 30s;
}

server
root
location
rewrite
try_files
proxy_passproxy_pass URI
疑问?️
To prevent a header field from being passed to the proxied server, set it to an empty string as follows:

location /some/path/ {
    proxy_set_header Accept-Encoding "";
    proxy_pass http://localhost:8000;
}```
proxy_set_header 
proxy_hide_header







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值