我用到的nginx

24次阅读

共计 2625 个字符,预计需要花费 7 分钟才能阅读完成。

查看 nginx 配置文件位置

方式 1
1) netstat -anop | grep 0.0.0.0:80 查看 nginx 的 pid
2) ll /proc/4562/exe nginx 运行的路径 (比如查询到: /usr/sbin/nginx)
3) /usr/sbin/nginx -t 查询 nginx 中 nginx.conf 文件位置
方式 2
nginx -t 直接查看位置

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

nginx 常用命令

验证配置是否正确: nginx -t (也能查询 nginx.conf 配置文件位置)
查看 Nginx 的版本号:nginx -V
启动 Nginx:start nginx
快速停止或关闭 Nginx:nginx -s stop
正常停止或关闭 Nginx:nginx -s quit
配置文件修改重装载命令:nginx -s reload(常用)
查看最后 30 行错误日志: tail -n30 /var/log/nginx/error.log

nginx 配置(请求转发,gzip)

# 每行代码必须; 结束

# 全局块
# nginx 进程 pid 存放路径,日志存放路径,配置文件引入,允许生成 worker process 数等
events { # events 块
  # 配置影响 nginx 服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。}

http {
  # http 全局块    
  # 可以嵌套多个 server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type 定义,日志自定义,是否使用 sendfile 传输文件,连接超时时间,单连接请求数等
  
  # 查看某个 url 有没有开启 gzip
  # curl -H "Accept-Encoding: gzip" -I url 地址   使用这个命令可以查看是否开启 gzip
  # 举例
  # curl -H "Accept-Encoding: gzip" -I http://47.100.11.151   这个是没有开启 gzip
  # HTTP/1.1 200 OK
  #    Server: nginx/1.10.2
  #    Date: Fri, 21 Jun 2019 22:49:09 GMT
  #    Content-Type: text/html
  #    Content-Length: 838
  #    Last-Modified: Sat, 16 Feb 2019 09:31:24 GMT
  #    Connection: keep-alive
  #    ETag: "5c67d86c-346"
  #    Accept-Ranges: bytes
  
  # curl -H "Accept-Encoding: gzip" -I https://www.jd.com  这个是开启 gzip(Content-Encoding: gzip)
  # HTTP/1.1 200 OK
  #    Server: JDWS/2.0
  #    Date: Fri, 21 Jun 2019 22:49:50 GMT
  #    Content-Type: text/html; charset=utf-8
  #    Content-Length: 30026
  #    Connection: keep-alive
  #    Vary: Accept-Encoding
  #    Expires: Fri, 21 Jun 2019 22:50:11 GMT
  #    Cache-Control: max-age=30
  #    Content-Encoding: gzip
  #    ser: 4.129
  #    Via: BJ-Y-NX-112(HIT), http/1.1 HZ-CT-1-JCS-25 ([cRs f])
  #    Age: 0
  #    Strict-Transport-Security: max-age=7776000
    
    
  # 开启 gzip 功能, 该功能可放在 http{},server{},location{}中, 优先级别 location > server > http
  gzip on; # 开启该功能
  gzip_min_length 1k; # 小于 1k 的文件将不会被 gzip
  gzip_comp_level 2; # 压缩级别,1-9, 数字越大, 文件压缩比越高, 压缩的越好
  # 以下文件类型将会被压缩, text/html 不用设置, 只要开启, 默认都会被压缩
  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 font/ttf font/otf image/svg+xml;
  gzip_vary on; # 是否在 http header 中添加 Vary: Accept-Encoding,建议开启
  gzip_disable "MSIE [1-6]\.";  # 在 IE6 中禁用 gzip
  server {
    # server 全局块, 做端口的监听
    # server 块:配置虚拟主机的相关参数,一个 http 中可以有多个 server
    location [path] {# location 块}
    location [path] { }
    
    # nginx 配置请求转发
    # 云服务器 IP: http://47.100.11.151  
    # 第三方 API: http://t.weather.sojson.com/api/weather/city/101030100
    # 跨域解决: 
    # 配置 1) proxy_pass http://t.weather.sojson.com/api/
    # 当访问 http://47.100.11.151/api/weather/city/101030100, 其实就是访问 http://t.weather.sojson.com/api/weather/city/101030100
    # 配置 2) proxy_pass 参数 http://t.weather.sojson.com  最后没有 /
    # 访问 http://47.100.11.151/api/weather/city/101030100, 其实就是访问 http://t.weather.sojson.com/api/weather/city/101030100
    # 推荐使用配置 2
    location /api/ {proxy_pass http://t.weather.sojson.com/api/;}
    
  }
} 

正文完
 0