Nginx目录和配置语法[运维教程]

Nginx目录和配置语法

Nginx目录和配置语法[运维教程]

指令
user 出于安全考虑,默认是nginx、nobody
worker_processes 工作进程数,一般来说,设置与CPU的核心数相同即可
error_log 保存错误日志的路径,可以设置error_log的级别
pid nginx 进程id
  1. user nginx;
  2. worker_processes 4;
  3. error_log /var/log/nginx/error.log warn;
  4. pid /var/run/nginx.pid;
  5. events {
  6. # 每秒处理多少个客户端连接
  7. worker_connections 1024;
  8. }
  9. http {
  10. include /etc/nginx/mime.types;
  11. default_type application/octet-stream;
  12. log_format main '$remote_addr - $remote_user [$time_local] '
  13. '"$request" $status $body_bytes_sent '
  14. '"$http_referer" "$http_user_agent"';
  15. access_log /var/log/nginx/access.log main;
  16. sendfile on;
  17. tcp_nopush on;
  18. #与客户端保持连接的最长时间
  19. keepalive_timeout 65;
  20. gzip on;
  21. include /etc/nginx/conf.d/*.conf;
  22. }

日志分析

  • ngxtop 流量实时监测 https://github.com/lebinh/ngxtop,这个简直就是神器(o^^o)
  • goaccess 日志可视化 https://github.com/allinurl/goaccess
  • goaccess 分析配置 https://github.com/stockrt/nginx2goaccess
  • goaccess 操作说明 https://www.fanhaobai.com/2017/06/go-access.html

日志分割

日积月累,nginx 的日志文件也会变得越来越大,如果我们是自己编译安装的nginx,可能就需要自己来处理日志文件的分割了。定时分割日志文件,这有利于降低单个文件的大小,方便排查,同时只保留最近一段时间的日志,也可以节省磁盘空间

一般采用 logrotate 来进行日志文件的分割:https://linux.die.net/man/8/logrotate

  1. # USR1 re-opening log files http://nginx.org/en/docs/control.html
  2. # daily 每天处理一次
  3. # missingok 忽略文件不存在的错误
  4. # rotate 设置旧文件保留的数量
  5. # compress 对日志进行压缩
  6. # delaycompress 延迟压缩
  7. # notifempty 如果文件内容为空,不处理
  8. # create 设置文件权限、用户、组
  9. # sharedscripts 对于prerotate和postrotate脚本,如果匹配到了多个日志文件,只运行一次脚本。如果没有匹配到,则不运行。
  10. # postrotate表示在日志rotate之后,执行的脚本
  11. /var/log/nginx/*.log {
  12. daily
  13. missingok
  14. rotate 30
  15. compress
  16. delaycompress
  17. notifempty
  18. create 640 nginx nginx
  19. sharedscripts
  20. postrotate
  21. if [ -f /var/run/nginx.pid ]; then
  22. kill -USR1 `cat /var/run/nginx.pid`
  23. fi
  24. endscript
  25. }

测试 logrotate 配置是否生效

logrotate -v /etc/logrotate.conf 调试定时任务,不会处理实际文件
logrotate -vf /etc/logrotate.conf 立刻执行所有任务,不管是否已到执行时间,会处理实际文件

server配置

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. #nginx程序默认的web根目录
  5. location / {
  6. root /usr/share/nginx/html;
  7. index index.html index.htm;
  8. }
  9. error_page 404 /404.html;
  10. # 重定向服务器的错误页到指定的静态页面 /50x.html
  11. error_page 500 502 503 504 404 /50x.html;
  12. location = /50x.html {
  13. root /usr/share/nginx/html;
  14. }
  15. location ~ .php$ {
  16. fastcgi_pass 127.0.0.1:9000;
  17. fastcgi_index index.php;
  18. include fastcgi_params;
  19. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  20. }
  21. }

当我们配置好了Nginx,就可以使用curl来进行初步的测试了:

  1. curl -v http://127.0.0.1/ > /dev/null

倘若我们想禁止用户通过IP或者未设置的域名来访问,可以采取如下措施:

  1. server {
  2. listen 80 default;
  3. rewrite ^(.*) http://www.example.com permanent;
  4. }

变量

变量类型 变量列表
内置变量 http://nginx.org/en/docs/varindex.html
HTTP请求变量 http://nginx.org/en/docs/http/ngx_http_core_module.html#var_status

参考文档:http://www.ttlsa.com/nginx/nginx-var-1/

路由

如果我们想要访问/path/to/name,那么 Nginx 配置中,路由匹配的优先级如下

修饰符 说明 优先级
location = /path/to/name 精确匹配 1
location /path/to/name 完整匹配 2
location ^~ /path/to/name 表示uri以某个常规字符串开头,非正则表达式 3
location ~* /path/to/name 使用不区分大小写的正则来进行匹配 4
location ~ /path/to/name 使用区分大小写的正则来进行匹配 4
location /path 部分匹配 5
location / 通用匹配 6
运维教程

nginx服务器的安装和配置[运维教程]

2020-5-15 16:04:13

运维教程

使用Let's Encrypt让域名支持HTTPS[运维教程]

2020-5-25 9:31:19

本站所发布的一切源码、模板、应用等文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版,购买注册,得到更好的正版服务。如有侵权。本站内容适用于DMCA政策。若您的权利被侵害,请与我们联系处理,站长 QQ: 84087680 或 点击右侧 私信:盾给网 反馈,我们将尽快处理。
⚠️
本站所发布的一切源码、模板、应用等文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版,购买注册,得到更好的正版服务。如有侵权。本站内容适用于DMCA政策
若您的权利被侵害,请与我们联系处理,站长 QQ: 84087680 或 点击右侧 私信:盾给网 反馈,我们将尽快处理。
0 条回复 A文章作者 M管理员
欢迎您,新朋友,感谢参与互动!
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
私信列表
搜索