您的位置:首页 > 财经 > 产业 > 减粘装置_滨州网站客服系统_关键词首页排名优化价格_竞价排名规则

减粘装置_滨州网站客服系统_关键词首页排名优化价格_竞价排名规则

2025/2/25 2:06:01 来源:https://blog.csdn.net/m0_66705547/article/details/143215378  浏览:    关键词:减粘装置_滨州网站客服系统_关键词首页排名优化价格_竞价排名规则
减粘装置_滨州网站客服系统_关键词首页排名优化价格_竞价排名规则

Nginx 的 server 配置块是 Nginx 配置文件中的一个关键部分,用于定义虚拟主机。每个 server 块可以包含多个 location 块和其他指令,以处理特定的请求。下面是对 server 配置块的详细解释:

一 server 配置块的基本结构

http {# 其他全局配置server {# 服务器配置}# 可以有多个 server 块
}

二 server 配置块中的常见指令

1. listen
  • 功能:指定 Nginx 监听的端口和 IP 地址。
  • 示例
    listen 80;  # 监听所有接口的 80 端口
    listen 192.168.1.1:80;  # 监听特定 IP 地址的 80 端口
2. server_name
  • 功能:指定该虚拟主机处理哪些域名。
  • 示例
    server_name example.com www.example.com;
3. root 和 alias
  • root:设置网站根目录。
    • 示例
      root /var/www/html;
  • alias:为特定位置设置别名。
    • 示例
      location /images/ {alias /data/images/;
      }
4. index
  • 功能:指定默认的索引文件。
  • 示例
    index index.html index.htm;
5. location
  • 功能:根据 URL 路径匹配不同的配置。
  • 示例
    location / {# 处理根路径
    }location /api/ {# 处理 /api/ 路径下的请求
    }location ~ \.php$ {# 处理 PHP 文件
    }
6. proxy_pass
  • 功能:将请求反向代理到后端服务器。
  • 示例
    location /api/ {proxy_pass http://backend_server;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;
    }
7. fastcgi_pass
  • 功能:将请求传递给 FastCGI 服务器(如 PHP-FPM)。
  • 示例
    location ~ \.php$ {fastcgi_pass   127.0.0.1:9000;fastcgi_index  index.php;fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;include        fastcgi_params;
    }
8. error_page
  • 功能:自定义错误页面。
  • 示例
    error_page  404              /404.html;
    location = /404.html {internal;
    }
9. access_log 和 error_log
  • access_log:记录访问日志。
    • 示例
      access_log /var/log/nginx/access.log main;
  • error_log:记录错误日志。
    • 示例
      error_log /var/log/nginx/error.log warn;
10. client_max_body_size
  • 功能:设置客户端请求的最大主体大小。
  • 示例
    client_max_body_size 10m;  # 设置最大上传文件大小为 10MB

完整的 server 配置块示例

以下是一个完整的 server 配置块示例,展示了如何配置一个简单的 Web 服务器,并处理静态文件、PHP 文件以及反向代理到后端 API 服务。

http {include       /etc/nginx/mime.types;default_type  application/octet-stream;log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log  /var/log/nginx/access.log  main;error_log   /var/log/nginx/error.log  warn;sendfile        on;keepalive_timeout  65;# 虚拟主机配置server {listen 80;server_name example.com www.example.com;# 根目录root /var/www/html;index index.html index.htm;# 处理静态文件location / {try_files $uri $uri/ =404;}# 处理 PHP 文件location ~ \.php$ {include snippets/fastcgi-php.conf;fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;}# 反向代理到后端 API 服务location /api/ {proxy_pass http://backend_server;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;}# 自定义错误页面error_page  404              /404.html;location = /404.html {internal;}}
}

解释

  • 监听 80 端口listen 80;
  • 处理域名server_name example.com www.example.com;
  • 根目录root /var/www/html;
  • 默认索引文件index index.html index.htm;
  • 处理静态文件location / { try_files $uri $uri/ =404; }
  • 处理 PHP 文件location ~ \.php$ { ... }
  • 反向代理到后端 API 服务location /api/ { ... }
  • 自定义 404 错误页面error_page 404 /404.html;

三 nginx server块匹配顺序 

在 Nginx 中,server 块的匹配顺序是基于请求的 Host 头和监听端口进行的。Nginx 会按照一定的规则来决定哪个 server 块应该处理当前的请求。以下是详细的匹配顺序和规则:

1. 监听端口匹配

首先,Nginx 会根据请求的端口号来选择合适的 server 块。例如,如果客户端请求的是 80 端口,那么 Nginx 会查找所有 listen 80;server 块。

2. server_name 匹配

在找到合适的端口后,Nginx 会进一步根据 Host 头(即 server_name)来选择具体的 server 块。server_name 的匹配顺序如下:

  1. 精确匹配

    • 首先检查是否有完全匹配的 server_name
    • 例如,如果 server_name 是 example.com,并且请求的 Host 头也是 example.com,则这个 server 块会被选中。
  2. 通配符匹配

    • 如果没有精确匹配,Nginx 会检查是否有以通配符开头或结尾的 server_name
    • 例如,server_name *.example.com 会匹配 sub.example.com
    • server_name example.* 会匹配 example.com 和 example.net
  3. 正则表达式匹配

    • 如果没有精确匹配或通配符匹配,Nginx 会检查是否有使用正则表达式的 server_name
    • 例如,server_name ~^(www\.)?example\.com$ 会匹配 example.com 和 www.example.com
  4. 默认服务器

    • 如果以上都没有匹配到,Nginx 会选择一个默认的 server 块。
    • 默认的 server 块是在指定端口上第一个定义的 server 块,或者显式指定了 default_server 参数的 server 块。
    • 例如:
      server {listen 80 default_server;server_name _;# 其他配置
      }

示例配置

假设你有以下 Nginx 配置文件:

http {# 第一个 server 块server {listen 80;server_name example.com;root /var/www/example;location / {try_files $uri $uri/ =404;}}# 第二个 server 块server {listen 80;server_name www.example.com;root /var/www/www_example;location / {try_files $uri $uri/ =404;}}# 第三个 server 块server {listen 80 default_server;server_name _;root /var/www/default;location / {try_files $uri $uri/ =404;}}# 第四个 server 块server {listen 80;server_name *.example.com;root /var/www/wildcard;location / {try_files $uri $uri/ =404;}}# 第五个 server 块server {listen 80;server_name ~^(www\.)?example\.com$;root /var/www/regex;location / {try_files $uri $uri/ =404;}}
}

请求示例

  • 请求 http://example.com/

    • Host 头为 example.com,精确匹配第一个 server 块。
    • 使用 /var/www/example 作为根目录。
  • 请求 http://www.example.com/

    • Host 头为 www.example.com,精确匹配第二个 server 块。
    • 使用 /var/www/www_example 作为根目录。
  • 请求 http://sub.example.com/

    • Host 头为 sub.example.com,匹配第四个 server 块(通配符匹配)。
    • 使用 /var/www/wildcard 作为根目录。
  • 请求 http://example.net/

    • Host 头为 example.net,没有任何匹配项,选择默认服务器(第三个 server 块)。
    • 使用 /var/www/default 作为根目录。
  • 请求 http://www.sub.example.com/

    • Host 头为 www.sub.example.com,正则表达式匹配第五个 server 块。
    • 使用 /var/www/regex 作为根目录。

 四 nginx location块匹配顺序

 

在 Nginx 中,location 块的匹配顺序是基于请求 URI 的,并且遵循一定的优先级规则。Nginx 会按照特定的顺序来选择最合适的 location 块来处理请求。以下是 location 块的匹配顺序和规则:

1. 精确匹配

  • 优先级最高:如果请求的 URI 完全匹配某个 location 块中的字符串,则该 location 块会被选中。
  • 示例
    location = /exact {# 处理精确匹配 /exact 的请求
    }

2. 前缀匹配

  • 普通前缀匹配:如果请求的 URI 以某个 location 块中的字符串开头,则该 location 块会被选中。
  • 示例
    location /prefix/ {# 处理以 /prefix/ 开头的请求
    }

3. 正则表达式匹配

  • 优先级较低:Nginx 会检查所有正则表达式 location 块,按配置文件中的顺序进行匹配。第一个匹配成功的 location 块会被选中。
  • 示例
    location ~ /regex/ {# 处理匹配正则表达式 /regex/ 的请求
    }

4. 特殊前缀匹配

  • 带有 ^~ 的前缀匹配:如果请求的 URI 以某个 location 块中的字符串开头,并且该 location 块使用了 ^~,则该 location 块会被选中,即使后面有更具体的正则表达式匹配。
  • 示例
    location ^~ /special-prefix/ {# 处理以 /special-prefix/ 开头的请求,忽略后面的正则表达式匹配
    }

匹配顺序总结

  1. 精确匹配 (=):优先级最高,完全匹配 URI。
  2. 特殊前缀匹配 (^~):次高优先级,匹配 URI 前缀,并忽略后续的正则表达式匹配。
  3. 普通前缀匹配:匹配 URI 前缀,但会被后续的正则表达式匹配覆盖。
  4. 正则表达式匹配 (~ 或 ~*):按配置文件中的顺序进行匹配,第一个匹配成功的 location 块被选中。

示例配置

假设你有以下 Nginx 配置文件:

server {listen 80;server_name example.com;# 精确匹配location = /exact {root /var/www/exact;}# 普通前缀匹配location /prefix/ {root /var/www/prefix;}# 特殊前缀匹配location ^~ /special-prefix/ {root /var/www/special_prefix;}# 正则表达式匹配location ~ /regex/ {root /var/www/regex;}# 默认 locationlocation / {root /var/www/default;}
}

请求示例

  • 请求 http://example.com/exact

    • 精确匹配 location = /exact
    • 使用 /var/www/exact 作为根目录。
  • 请求 http://example.com/prefix/somefile.html

    • 普通前缀匹配 location /prefix/
    • 使用 /var/www/prefix 作为根目录。
  • 请求 http://example.com/special-prefix/somefile.html

    • 特殊前缀匹配 location ^~ /special-prefix/
    • 使用 /var/www/special_prefix 作为根目录。
  • 请求 http://example.com/regex/somefile.html

    • 正则表达式匹配 location ~ /regex/
    • 使用 /var/www/regex 作为根目录。
  • 请求 http://example.com/otherfile.html

    • 默认 location /
    • 使用 /var/www/default 作为根目录。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com