SmartSoftHelp 服务器性能优化之Win IIS,Nginx for Linux,Nginx for Windows 深度优化版:SmartSoftHelp DeepCore XSuite

SmartSoftHelp  之 Ser 服务器性能优化 (IISNginxDeployMent) 菜单
服务器性能优化 Windows IIS,Nginx for Linux,Nginx for Windows 优化配置方案

1. 应用程序池优化

xml

<configuration>
  <system.applicationHost>
    <applicationPools>
      <add name="YourAppPool" managedRuntimeVersion="v4.0">
        <processModel 
          maxConcurrentRequestsPerCPU="5000" 
          maxConcurrentThreadsPerCPU="0" 
          requestQueueLimit="5000" />
        <recycling logEventOnRecycle="Time,Requests,Schedule,Memory,IsapiUnhealthy,OnDemand,ConfigChange,PrivateMemory">
          <periodicRestart time="0" />
        </recycling>
      </add>
    </applicationPools>
  </system.applicationHost>
</configuration>
2. 请求过滤与静态内容处理

xml

<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="1073741824" />
    </requestFiltering>
  </security>
  <staticContent>
    <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
  </staticContent>
</system.webServer>
3. 启用 HTTP/2 和压缩

powershell

# 启用 HTTP/2
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\HTTP\Parameters -Name EnableHttp2Protocol -Value 1

# 启用动态和静态压缩
Add-WindowsFeature Web-Server,Web-Common-Http,Web-Default-Doc,Web-Dir-Browsing,Web-Http-Errors,Web-Static-Content,Web-Http-Logging,Web-Stat-Compression,Web-Dyn-Compression

Nginx for Linux 性能优化配置方案

1. 核心 Nginx 配置优化

nginx

worker_processes auto;
worker_cpu_affinity auto;
worker_rlimit_nofile 65535;

events {
    worker_connections 65535;
    multi_accept on;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    
    keepalive_timeout 65;
    keepalive_requests 100;
    
    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    
    server_tokens off;
    types_hash_max_size 2048;
    
    # 负载均衡配置
    upstream backend {
        least_conn;
        server backend1.example.com weight=5;
        server backend2.example.com weight=5;
        keepalive 32;
    }
}
2. Linux 系统参数优化

bash

# /etc/sysctl.conf
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.ip_local_port_range = 1024 65535

# 应用配置
sysctl -p
3. 文件描述符限制

bash

# /etc/security/limits.conf
nginx   hard    nofile  65535
nginx   soft    nofile  65535

# /etc/systemd/system/nginx.service.d/override.conf
[Service]
LimitNOFILE=65535

Nginx for Windows 性能优化配置方案

1. 基础 Nginx 配置

nginx

worker_processes 4;
worker_connections 16384;

events {
    use epoll;
    multi_accept on;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    
    keepalive_timeout 65;
    keepalive_requests 100;
    
    gzip on;
    gzip_types text/plain text/css application/json application/javascript;
    
    # 静态文件缓存
    open_file_cache max=1000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;
}
2. Windows 系统优化

powershell

# 增加 TCP 连接数限制
netsh int tcp set global autotuninglevel=normal
netsh int tcp set global rss=enabled
netsh int tcp set global chimney=enabled
netsh int tcp set global congestionprovider=ctcp

# 调整注册表参数
reg add "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" /v MaxUserPort /t REG_DWORD /d 65534 /f
reg add "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" /v TcpTimedWaitDelay /t REG_DWORD /d 30 /f
3. 性能监控与维护

powershell

# 性能监控脚本
function Get-NginxCounters {
    $nginxProcess = Get-Process nginx -ErrorAction SilentlyContinue
    if ($nginxProcess) {
        $cpu = (Get-Counter "\Process(nginx*)\% Processor Time").CounterSamples.CookedValue
        $mem = (Get-Counter "\Process(nginx*)\Working Set - Private").CounterSamples.CookedValue
        [PSCustomObject]@{
            CPU = "$($cpu)%"
            Memory = "$([math]::Round($mem/1MB,2)) MB"
            Processes = $nginxProcess.Count
        }
    } else {
        Write-Warning "Nginx 进程未运行"
    }
}

综合优化建议

  1. 负载均衡配置

nginx

upstream backend_servers {
    ip_hash;
    server backend1.example.com:8080 max_fails=3 fail_timeout=30s;
    server backend2.example.com:8080 max_fails=3 fail_timeout=30s;
    server backend3.example.com:8080 backup;
}

server {
    listen 80;
    server_name example.com;
    
    location / {
        proxy_pass http://backend_servers;
        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;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
    }
}

  1. 缓存策略配置

nginx

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires 7d;
    log_not_found off;
    access_log off;
}

location ~* \.(pdf|doc|docx|xls|xlsx|ppt|pptx)$ {
    expires 30d;
}

  1. 安全与性能兼顾配置

nginx

# 限制请求速率
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;

server {
    # ...其他配置...
    
    location /api/ {
        limit_req zone=mylimit burst=20 nodelay;
    }
    
    # 防止 DDOS 攻击
    limit_conn_zone $binary_remote_addr zone=perip:10m;
    limit_conn perip 100;
}

以上配置方案覆盖了服务器软件和操作系统层面的优化,实际应用时应根据服务器硬件配置和应用特性进行调整。建议在测试环境验证后再应用到生产环境。

以上内容大模型比较浅的分析!

SmartSoftHelp   终结者提供比较全面深入,专业的优化方案!

请到Ser 服务器性能优化 (IISNginxDeployMent) 菜单   有更加详细深入透彻的分析!

 下载地址:

1.GitHub(托管)   GitHub - 512929249/smartsofthelp: SmartSoftHelp DeepCore XSuite 做世界一流的,最好的,最优秀,最简单,最流畅,最实用的.Net C#辅助开发工具SmartSoftHelp DeepCore XSuite 做世界一流的,最好的,最优秀,最简单,最流畅,最实用的.Net C#辅助开发工具 - 512929249/smartsofthelphttps://github.com/512929249/smartsofthelp.git


2.Gitee(码云)      SmartSoftHelp: SmartSoftHelp DeepCore XSuite做世界一流的,最好的,最优秀,最简单,最流畅,最实用的.Net C#辅助开发工具https://gitee.com/sky512929249/smartsofthelp.git

最优秀

最专业

最全面的

开发使用工具 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值