您的位置: 首页 - 站长

wordpress个人网站后台登陆在线crm

当前位置: 首页 > news >正文

wordpress个人网站后台登陆,在线crm,网络营销导向网站建设的基础是什么,网页设计教程步骤Nginx 的 server 配置块是 Nginx 配置文件中的一个关键部分#xff0c;用于定义虚拟主机。每个 server 块可以包含多个 location 块和其他指令#xff0c;以处理特定的请求。下面是对 server 配置块的详细解释#xff1a; 一 server 配置块的基本结构 http {# 其他全局配置…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/; } 生产常用技巧 防止意外的目录遍历 使用 alias 的安全性 alias 指令通过替换请求 URI 的部分来直接映射到文件系统中的一个特定目录。这种方式可以有效防止目录遍历攻击因为它不会保留 URI 中的路径结构。 示例 假设你有以下目录结构 /var/www/ ├── static │   └── logo.png └── images     └── banner.jpg 配置如下 server {     listen 80;     server_name example.com;     location /static/ {         alias /var/www/static/;     }     location /images/ {         alias /var/www/images/;     } } 请求 /static/logo.png 会映射到 /var/www/static/logo.png。 请求 /static/../images/banner.jpg 不会被解析为 /var/www/images/banner.jpg而是会返回 404 错误。 这是因为 alias 会将 /static/ 替换为 /var/www/static/而不会保留 URI 中的其他部分。因此/static/../images/banner.jpg 实际上会被解析为 /var/www/static/../images/banner.jpg这在文件系统中是无效的路径Nginx 会返回 404 错误。 使用 root 的潜在风险 root 指令将请求的 URI 附加到 root 指定的路径后面。这种方式可能会导致目录遍历攻击因为 URI 中的路径结构会被保留。 示例 假设你有相同的目录结构并且配置如下 server {     listen 80;     server_name example.com;     location /static/ {         root /var/www/;     }     location /images/ {         root /var/www/;     } } 请求 /static/logo.png 会映射到 /var/www/static/logo.png。 请求 /static/../images/banner.jpg 会被解析为 /var/www/static/../images/banner.jpg这在文件系统中是有效的路径指向 /var/www/images/banner.jpg。 这种情况下恶意用户可以通过构造特定的 URL 来访问服务器上的其他文件从而可能导致敏感信息泄露或其他安全问题。 如何防止目录遍历攻击 即使使用 root 指令也可以通过一些额外的配置来防止目录遍历攻击。以下是一些常用的方法 使用 try_files 指令 location /static/ {     root /var/www/;     try_files \(uri 404; } 这样如果请求的文件不存在Nginx 会返回 404 错误而不是尝试访问其他路径。 使用 internal 关键字 location /internal/ {     internal;  # 只允许内部重定向     root /var/www/internal/; } 这样只有内部重定向如 rewrite 或 proxy_pass才能访问这个位置外部请求会被拒绝。 限制访问的文件类型 location /static/ {     root /var/www/;     if (\)request_uri ~* .(php|pl|py|sh|cgi)\() {         return 403;     } } 这样可以防止访问特定类型的文件例如脚本文件。 使用 location 块进行更精细的控制 location /static/ {     root /var/www/;     location ~* \..*/.*\.php\) {  # 匹配以 . 开头的路径并包含 .php 的请求         deny all;     } } 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 的匹配顺序如下 精确匹配 首先检查是否有完全匹配的 server_name。例如如果 server_name 是 example.com并且请求的 Host 头也是 example.com则这个 server 块会被选中。 通配符匹配 如果没有精确匹配Nginx 会检查是否有以通配符开头或结尾的 server_name。例如server_name *.example.com 会匹配 sub.example.com。server_name example.* 会匹配 example.com 和 example.net。 正则表达式匹配 如果没有精确匹配或通配符匹配Nginx 会检查是否有使用正则表达式的 server_name。例如server_name ~^(www.)?example.com\( 会匹配 example.com 和 www.example.com。 默认服务器 如果以上都没有匹配到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 块的匹配顺序和规则
  2. 精确匹配 优先级最高如果请求的 URI 完全匹配某个 location 块中的字符串则该 location 块会被选中。示例 location /exact {# 处理精确匹配 /exact 的请求 }
  3. 前缀匹配 普通前缀匹配如果请求的 URI 以某个 location 块中的字符串开头则该 location 块会被选中。示例 location /prefix/ {# 处理以 /prefix/ 开头的请求 }
  4. 正则表达式匹配 优先级较低Nginx 会检查所有正则表达式 location 块按配置文件中的顺序进行匹配。第一个匹配成功的 location 块会被选中。示例 location ~ /regex/ {# 处理匹配正则表达式 /regex/ 的请求 }
  5. 特殊前缀匹配 带有 ^~ 的前缀匹配如果请求的 URI 以某个 location 块中的字符串开头并且该 location 块使用了 ^则该 location 块会被选中即使后面有更具体的正则表达式匹配。示例 location ^ /special-prefix/ {# 处理以 /special-prefix/ 开头的请求忽略后面的正则表达式匹配 }
    匹配顺序总结 精确匹配 ()优先级最高完全匹配 URI。特殊前缀匹配 (^~)次高优先级匹配 URI 前缀并忽略后续的正则表达式匹配。普通前缀匹配匹配 URI 前缀但会被后续的正则表达式匹配覆盖。正则表达式匹配 ( 或 *)按配置文件中的顺序进行匹配第一个匹配成功的 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 作为根目录。