查看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 oknginx: 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/;    }      }}