典型配置

  1. # main段配置信息
  2. user nginx; # 运行用户,默认即是nginx,可以不进行设置
  3. worker_processes auto; # Nginx 进程数,一般设置为和 CPU 核数一样
  4. error_log /var/log/nginx/error.log warn; # Nginx 的错误日志存放目录
  5. pid /var/run/nginx.pid; # Nginx 服务启动时的 pid 存放位置
  6. # events段配置信息
  7. events {
  8. use epoll; # 使用epollI/O模型(如果你不知道Nginx该使用哪种轮询方法,会自动选择一个最适合你操作系统的)
  9. worker_connections 1024; # 每个进程允许最大并发数
  10. }
  11. # http段配置信息
  12. # 配置使用最频繁的部分,代理、缓存、日志定义等绝大多数功能和第三方模块的配置都在这里设置
  13. http {
  14. # 设置日志模式
  15. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  16. '$status $body_bytes_sent "$http_referer" '
  17. '"$http_user_agent" "$http_x_forwarded_for"';
  18. access_log /var/log/nginx/access.log main; # Nginx访问日志存放位置
  19. sendfile on; # 开启高效传输模式
  20. tcp_nopush on; # 减少网络报文段的数量
  21. tcp_nodelay on;
  22. keepalive_timeout 65; # 保持连接的时间,也叫超时时间,单位秒
  23. types_hash_max_size 2048;
  24. include /etc/nginx/mime.types; # 文件扩展名与类型映射表
  25. default_type application/octet-stream; # 默认文件类型
  26. include /etc/nginx/conf.d/*.conf; # 加载子配置项
  27. }
  28. # server段配置信息
  29. server {
  30. listen 80; # 配置监听的端口
  31. server_name localhost; # 配置的域名
  32. # location段配置信息
  33. location / {
  34. root /usr/share/nginx/html; # 网站根目录
  35. index index.html index.htm; # 默认首页文件
  36. deny 172.168.22.11; # 禁止访问的ip地址,可以为all
  37. allow 172.168.33.44;# 允许访问的ip地址,可以为all
  38. }
  39. error_page 500 502 503 504 /50x.html; # 默认50x对应的访问页面
  40. error_page 400 404 error.html; # 同上
  41. }
  42. stream {
  43. #nginx代理172.18.103.197:8899服务
  44. upstream backend {
  45. server 172.18.103.197:8899 weight=1;
  46. }
  47. server {
  48. listen 9001;
  49. proxy_pass backend;
  50. }
  51. #nginx代理172.18.103.197:8489服务
  52. server {
  53. listen 8489;
  54. proxy_pass 172.18.103.197:8489;
  55. }
  56. server {
  57. listen 8848;
  58. proxy_pass 172.18.103.197:8848;
  59. }
  60. }

- main:全局配置,对全局生效;
- events:配置影响 Nginx 服务器与用户的网络连接;
- http:配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置;
- server:配置虚拟主机的相关参数,一个 http 块中可以有多个 server 块;
- location:用于配置匹配的 uri ;
- upstream:配置后端服务器具体地址,负载均衡配置不可或缺的部分。
image.png

main段配置

  1. #user USERNAME [GROUP] #运行用户名 用户组可选
  2. user nginx; # 运行用户,默认即是nginx,可以不进行设置
  3. pid /var/run/nginx.pid; # Nginx 服务启动时的 pid 存放位置
  4. worker_rlimit_nofile 20480; # 可以理解成每个worker子进程的最大连接数量。
  5. worker_rlimit_core 50M; # 存放大小限制
  6. working_directory /opt/nginx/tmp; # 存放目录
  7. worker_processes auto; # Nginx 进程数,一般设置为和 CPU 核数一样
  8. error_log /var/log/nginx/error.log warn; # Nginx 的错误日志存放目录
  9. worker_priority -10; # 120-10=110110就是最终的优先级
  10. worker_shutdown_timeout 5s;
  11. timer_resolution 100ms;
  12. daemon off; # 默认是on,后台运行模式

user:指定运行 Nginx 的 woker 子进程的属主和属组,其中组可以不指定。
pid:指定运行 Nginx master 主进程的 pid 文件存放路径。
worker_rlimit_nofile_number:指定 worker 子进程可以打开的最大文件句柄数。
worker_rlimit_core:指定 worker 子进程异常终止后的 core 文件,用于记录分析问题。
worker_processes_number:指定 Nginx 启动的 worker 子进程数量。
worker_cpu_affinity:将每个 worker 子进程与我们的 cpu 物理核心绑定。将每个 worker 子进程与特定 CPU 物理核心绑定,优势在于,避免同一个 worker 子进程在不同的 CPU 核心上切换,缓存失效,降低性能。但其并不能真正的避免进程切换
image.png
worker_priority:指定 worker 子进程的 nice 值,以调整运行 Nginx 的优先级,通常设定为负值,以优先调用 Nginx 。(Linux 默认进程的优先级值是120,值越小越优先;nice 定范围为 -20 到 +19 )应用的默认优先级值是120加上 nice 值等于它最终的值,这个值越小,优先级越高。
worker_shutdown_timeout:指定 worker 子进程优雅退出时的超时时间。
timer_resolution:worker 子进程内部使用的计时器精度,调整时间间隔越大,系统调用越少,有利于性能提升;反之,系统调用越多,性能下降。
daemon:指定 Nginx 的运行方式,前台还是后台,前台用于调试,后台用于生产。

events端配置

use

Nginx 使用何种事件驱动模型。

  1. use method; # 不推荐配置它,让nginx自己选择method 可选值为:selectpollkqueueepoll、/dev/polleventport

worker_connections

worker 子进程能够处理的最大并发连接数。

  1. worker_connections 1024 # 每个子进程的最大连接数为1024

accept_mutex

是否打开负载均衡互斥锁。

  1. accept_mutex on # 默认是off关闭的,这里推荐打开

server_name 指令

指定虚拟主机域名。

  1. server_name name1 name2 name3
  2. # 示例:server_name www.nginx.com;

域名匹配的四种写法:

  • 精确匹配:server_name www.nginx.com ;
  • 左侧通配:server_name *.nginx.com ;
  • 右侧统配:server_name www.nginx.* ;
  • 正则匹配:server_name ~^www.nginx.*$ ;

匹配优先级:精确匹配 > 左侧通配符匹配 > 右侧通配符匹配 > 正则表达式匹配

server_name 配置实例

1) 配置本地 DNS 解析 vim /etc/hosts

  1. # 添加如下内容,其中 121.42.11.34 是阿里云服务器IP地址
  2. 121.42.11.34 www.nginx-test.com
  3. 121.42.11.34 mail.nginx-test.com
  4. 121.42.11.34 www.nginx-test.org
  5. 121.42.11.34 doc.nginx-test.com
  6. 121.42.11.34 www.nginx-test.cn
  7. 121.42.11.34 fe.nginx-test.club

[注意] 这里使用的是虚拟域名进行测试,因此需要配置本地 DNS 解析,如果使用阿里云上购买的域名,则需要在阿里云上设置好域名解析。

2) 配置Nginx ,vim /etc/nginx/nginx.conf

  1. # 列举http端中的sever端配置
  2. # 左匹配
  3. server {
  4. listen 80;
  5. server_name *.nginx-test.com;
  6. root /usr/share/nginx/html/nginx-test/left-match/;
  7. location / {
  8. index index.html;
  9. }
  10. }
  11. # 正则匹配
  12. server {
  13. listen 80;
  14. server_name ~^.*\.nginx-test\..*$;
  15. root /usr/share/nginx/html/nginx-test/reg-match/;
  16. location / {
  17. index index.html;
  18. }
  19. }
  20. # 右匹配
  21. server {
  22. listen 80;
  23. server_name www.nginx-test.*;
  24. root /usr/share/nginx/html/nginx-test/right-match/;
  25. location / {
  26. index index.html;
  27. }
  28. }
  29. # 完全匹配
  30. server {
  31. listen 80;
  32. server_name www.nginx-test.com;
  33. root /usr/share/nginx/html/nginx-test/all-match/;
  34. location / {
  35. index index.html;
  36. }
  37. }

3) 访问分析

  • 当访问 www.nginx-test.com 时,都可以被匹配上,因此选择优先级最高的“完全匹配”;
  • 当访问 mail.nginx-test.com 时,会进行“左匹配”;
  • 当访问 www.nginx-test.org 时,会进行“右匹配”;
  • 当访问 doc.nginx-test.com 时,会进行“左匹配”;
  • 当访问 www.nginx-test.cn 时,会进行“右匹配”;
  • 当访问 fe.nginx-test.club 时,会进行“正则匹配”。

    root

    指定静态资源目录位置,它可以写在 http 、 server 、 location 等配置中。 ```java root path

例如: location /image { root /opt/nginx/static; }

当用户访问 www.test.com/image/1.png 时,实际在服务器找的路径是 /opt/nginx/static/image/1.png

  1. [注意] root 会将定义路径与 URI 叠加, alias 则只取定义路径。
  2. <a name="HIBib"></a>
  3. ## alias
  4. 指定静态资源目录位置,它**只能写在 location** 中。
  5. ```java
  6. location /image {
  7. alias /opt/nginx/static/image/;
  8. }
  9. 当用户访问 www.test.com/image/1.png 时,实际在服务器找的路径是 /opt/nginx/static/image/1.png

[注意] 使用 alias 末尾一定要添加 / ,并且它只能位于 location 中。

location

配置路径。
location [ = | ~ | ~* | ^~ ] uri { …}
匹配规则:

  • = 精确匹配;
  • ~ 正则匹配,区分大小写;
  • ~* 正则匹配,不区分大小写;
  • ^~ 匹配到即停止搜索;

匹配优先级:= > ^~ > ~ > ~* > 不带任何字符。

配置实例

  1. server {
  2. listen 80;
  3. server_name www.nginx-test.com;
  4. # 只有当访问 www.nginx-test.com/match_all/ 时才会匹配到/usr/share/nginx/html/match_all/index.html
  5. location = /match_all/ {
  6. root /usr/share/nginx/html
  7. index index.html
  8. }
  9. # 当访问 www.nginx-test.com/1.jpg 等路径时会去 /usr/share/nginx/images/1.jpg 找对应的资源
  10. location ~ \.(jpeg|jpg|png|svg)$ {
  11. root /usr/share/nginx/images;
  12. }
  13. # 当访问 www.nginx-test.com/bbs/ 时会匹配上 /usr/share/nginx/html/bbs/index.html
  14. location ^~ /bbs/ {
  15. root /usr/share/nginx/html;
  16. index index.html index.htm;
  17. }
  18. }

1) location 中的反斜线

  1. location /test {
  2. ...
  3. }
  4. location /test/ {
  5. ...
  6. }
  • 不带 / 当访问 www.nginx-test.com/test 时, Nginx 先找是否有 test 目录,如果有则找 test 目录下的 index.html ;如果没有 test 目录, nginx 则会找是否有 test 文件;
  • 带 / 当访问 www.nginx-test.com/test 时, Nginx 先找是否有 test 目录,如果有则找 test 目录下的 index.html ,如果没有它也不会去找是否存在 test 文件。

    2) return

    停止处理请求,直接返回响应码或重定向到其他 URL ;执行 return 指令后, location 中后续指令将不会被执行。 ```java

return code [text]; return code URL; return URL;

例如: location / { return 404; # 直接返回状态码 }

location / { return 404 “pages not found”; # 返回状态码 + 一段文本 }

location / { return 302 /bbs ; # 返回状态码 + 重定向地址 }

location / { return https://www.baidu.com ; # 返回重定向地址 }

  1. <a name="ql9pU"></a>
  2. ## rewrite
  3. 根据指定正则表达式匹配规则,重写 URL 。<br />语法:rewrite 正则表达式 要替换的内容 [flag];<br />上下文:server、location、if<br />示例:rewirte /images/(.*\.jpg)$ /pic/$1; _# $1是前面括号(.*\.jpg)的反向引用_
  4. flag 可选值的含义:
  5. - last 重写后的 URL 发起新请求,再次进入 server 段,重试 location 的中的匹配;
  6. - break 直接使用重写后的 URL ,不再匹配其它 location 中语句;
  7. - redirect 返回 302 临时重定向;
  8. - permanent 返回 301 永久重定向。
  9. ```java
  10. server{
  11. listen 80;
  12. server_name fe.lion.club; # 要在本地hosts文件进行配置
  13. root html;
  14. location /search {
  15. rewrite ^/(.*) https://www.baidu.com redirect;
  16. }
  17. location /images {
  18. rewrite /images/(.*) /pics/$1;
  19. }
  20. location /pics {
  21. rewrite /pics/(.*) /photos/$1;
  22. }
  23. location /photos {
  24. }
  25. }

按照这个配置我们来分析:

  • 当访问 fe.lion.club/search 时,会自动帮我们重定向到 https://www.baidu.com;
  • 当访问 fe.lion.club/images/1.jpg 时,第一步重写 URL 为 fe.lion.club/pics/1.jpg ,找到 pics 的 location ,继续重写 URL 为 fe.lion.club/photos/1.jpg ,找到 /photos 的 location 后,去 html/photos 目录下寻找 1.jpg 静态资源。

    if 指令

    ```java

语法:if (condition) {…}

上下文:server、location

示例: if($http_user_agent ~ Chrome){ rewrite /(.*)/browser/$1 break; }

  1. condition 判断条件:
  2. - **$variable** 仅为变量时,值为空或以0开头字符串都会被当做 false 处理;
  3. - **=** **!=** 相等或不等;
  4. - **~** 正则匹配;
  5. - **! ~** 非正则匹配;
  6. - **~*** 正则匹配,不区分大小写;
  7. - **-f** **! -f** 检测文件存在或不存在;
  8. - **-d **或 **! -d** 检测目录存在或不存在;
  9. - **-e** **! -e** 检测文件、目录、符号链接等存在或不存在;
  10. - **-x** **! -x** 检测文件可以执行或不可执行;
  11. 实例:
  12. ```java
  13. server {
  14. listen 8080;
  15. server_name localhost;
  16. root html;
  17. location / {
  18. if ( $uri = "/images/" ){
  19. rewrite (.*) /pics/ break;
  20. }
  21. }
  22. }

当访问 localhost:8080/images/ 时,会进入 if 判断里面执行 rewrite 命令。

autoindex

用户请求以 / 结尾时,列出目录结构,可以用于快速搭建静态资源下载网站。
autoindex.conf 配置信息:

  1. server {
  2. listen 80;
  3. server_name fe.lion-test.club;
  4. location /download/ {
  5. root /opt/source;
  6. autoindex on; # 打开 autoindex,,可选参数有 on | off
  7. autoindex_exact_size on; # 修改为off,以KBMBGB显示文件大小,默认为on,以bytes显示出⽂件的确切⼤⼩
  8. autoindex_format html; # html的方式进行格式化,可选参数有 html | json | xml
  9. autoindex_localtime off; # 显示的⽂件时间为⽂件的服务器时间。默认为off,显示的⽂件时间为GMT时间
  10. }
  11. }

当访问 fe.lion.com/download/ 时,会把服务器 /opt/source/download/ 路径下的文件展示出来,如下图所示:
image.png

变量

Nginx 提供给使用者的变量非常多,但是终究是一个完整的请求过程所产生数据, Nginx 将这些数据以变量的形式提供给使用者。
image.png

  1. server{
  2. listen 8081;
  3. server_name var.lion-test.club;
  4. root /usr/share/nginx/html;
  5. location / {
  6. return 200 "
  7. remote_addr: $remote_addr
  8. remote_port: $remote_port
  9. server_addr: $server_addr
  10. server_port: $server_port
  11. server_protocol: $server_protocol
  12. binary_remote_addr: $binary_remote_addr
  13. connection: $connection
  14. uri: $uri
  15. request_uri: $request_uri
  16. scheme: $scheme
  17. request_method: $request_method
  18. request_length: $request_length
  19. args: $args
  20. arg_pid: $arg_pid
  21. is_args: $is_args
  22. query_string: $query_string
  23. host: $host
  24. http_user_agent: $http_user_agent
  25. http_referer: $http_referer
  26. http_via: $http_via
  27. request_time: $request_time
  28. https: $https
  29. request_filename: $request_filename
  30. document_root: $document_root
  31. ";
  32. }
  33. }

当我们访问 http://var.lion-test.club:8081/test?pid=121414&cid=sadasd 时,由于 Nginx 中写了 return 方法,因此 chrome 浏览器会默认为我们下载一个文件,下面展示的就是下载的文件内容:

  1. remote_addr: 27.16.220.84
  2. remote_port: 56838
  3. server_addr: 172.17.0.2
  4. server_port: 8081
  5. server_protocol: HTTP/1.1
  6. binary_remote_addr:
  7. connection: 126
  8. uri: /test/
  9. request_uri: /test/?pid=121414&cid=sadasd
  10. scheme: http
  11. request_method: GET
  12. request_length: 518
  13. args: pid=121414&cid=sadasd
  14. arg_pid: 121414
  15. is_args: ?
  16. query_string: pid=121414&cid=sadasd
  17. host: var.lion-test.club
  18. http_user_agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36
  19. http_referer:
  20. http_via:
  21. request_time: 0.000
  22. https:
  23. request_filename: /usr/share/nginx/html/test/
  24. document_root: /usr/share/nginx/html