nginx https反向代理太慢

发布于 2025-02-08 13:01:09 字数 5939 浏览 4 评论 0原文

我已经在CentOS中使用HTTPS反向代理实现了NGINX缓存,每个请求的响应时间需要超过1.5秒。我的NGINX服务器配置是4个核心,8GB RAM。

我的配置看起来像下面的(nginx.config)

`user nginx;
worker_processes auto;
worker_rlimit_nofile 100000;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

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


http {
   
    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 10m;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    log_format rt_cache '$remote_addr - $upstream_cache_status [$time_local]  '
                      '"$request" $status $body_bytes_sent '
                      '"$http_referer" "$http_user_agent"';

    # Below pattern will print
    # Time stamp | Client IP | client Dev apps Name| Request | Status Returned| Time taken in ms| size Returned in bytes| Referer | hit or miss | User agent
    log_format bf_log_format '[$time_local]|'
                             '$remote_addr|'
                             '$http_x_developer_username|$http_x_forwarded_for|'
                             '"$request"|'
                             '$status|$upstream_response_time|$body_bytes_sent|'
                             '"$http_referer"|'
                             '"$upstream_cache_status"|'
                             '"$http_user_agent"';

     log_format json_log_format escape=json '{'
                                 '"time": "$time_iso8601",'
                                 '"trace_id": "$request_id",'
                                  '"http": {'
                                     '"body_bytes_sent": "$body_bytes_sent",'
                                     '"x_developer_username": "$http_x_developer_username",'
                                     '"remote_addr": "$remote_addr",'
                                     '"method": "$request_method",'
                                     '"request": "$request_uri",'
                                     '"schema": "$scheme",'
                                     '"request_time": "$request_time",'
                                     '"host": "$host",'
                                     '"uri": "$uri",'
                                     '"user_agent": "$http_user_agent",'
                                     '"status": "$status"'
                                  '},'
                                  '"proxy": {'
                                     '"host": "$proxy_host"'
                                  '},'
                                  '"upstream": {'
                                     '"response_time": "$upstream_response_time sec",'
                                     '"cache_status": "$upstream_cache_status"'
                                   '}'
                                 '}';

   #  access_log  /var/log/nginx/access.log  main;
#    access_log   /var/log/nginx/access.log json_log_format;
    access_log off;

    sendfile            on;
    sendfile_max_chunk 512k;
    # directio 4m;
    # directio_alignment 512;
    tcp_nopush          on;
    tcp_nodelay         on;

    reset_timedout_connection on;

    keepalive_requests 100000;
    types_hash_max_size 2048;

    # reduce the data that needs to be sent over network -- for testing environment
    gzip on;
    # gzip_static on;
    gzip_min_length 10240;
    gzip_comp_level 1;
    gzip_vary on;
    gzip_disable msie6;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types
        text/css
        text/javascript
        text/xml
        text/plain
        text/x-component
        application/javascript
        application/x-javascript
        application/json
        application/xml
        application/rss+xml
        application/atom+xml
        font/truetype
        font/opentype
        application/vnd.ms-fontobject
        image/svg+xml;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.


    proxy_cache_path /opt/nginx/cache levels=1:2 keys_zone=api-cache:3000m max_size=100g inactive=43200m use_temp_path=off;
    proxy_temp_path /opt/nginx/cache/other;

    include /etc/nginx/conf.d/ssl.conf;
}` 

我的ssl.confg如下所示

 server {

    server_name  _;
    root         /usr/share/nginx/html;

    listen       443 ssl http2 default_server;
    listen       [::]:443 ssl;

    ssl_certificate     "/etc/private/ssl/cert.pem";
    ssl_certificate_key "/etc/private/ssl/key.pem";
    # ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    keepalive_timeout   100;


    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

    location /health {
       default_type application/json;
       return 200 '{"status":"UP"}';
    }
    location /nginx-status {
        stub_status;
    }

    location /trellotest {
        proxy_cache_bypass $http_no_cache_purge $arg_nocache;
        proxy_cache_methods GET POST;
        add_header Cache-Control "public";
        proxy_cache api-cache;
        proxy_cache_valid 200 40320m;
        add_header X-Cache $upstream_cache_status;
        add_header X-Time $request_time;
        proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
        proxy_pass https://mytrelloapp;
    }
}

,如果可能的话,您是否可以建议我是否有任何改进上述配置?

I have Implemented the Nginx cache with https reverse proxy in centos, My response time taking more than 1.5 seconds for each request. My nginx server configuration was 4 core, 8gb ram.

My configuration looks like below (nginx.config)

`user nginx;
worker_processes auto;
worker_rlimit_nofile 100000;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

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


http {
   
    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 10m;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    log_format rt_cache '$remote_addr - $upstream_cache_status [$time_local]  '
                      '"$request" $status $body_bytes_sent '
                      '"$http_referer" "$http_user_agent"';

    # Below pattern will print
    # Time stamp | Client IP | client Dev apps Name| Request | Status Returned| Time taken in ms| size Returned in bytes| Referer | hit or miss | User agent
    log_format bf_log_format '[$time_local]|'
                             '$remote_addr|'
                             '$http_x_developer_username|$http_x_forwarded_for|'
                             '"$request"|'
                             '$status|$upstream_response_time|$body_bytes_sent|'
                             '"$http_referer"|'
                             '"$upstream_cache_status"|'
                             '"$http_user_agent"';

     log_format json_log_format escape=json '{'
                                 '"time": "$time_iso8601",'
                                 '"trace_id": "$request_id",'
                                  '"http": {'
                                     '"body_bytes_sent": "$body_bytes_sent",'
                                     '"x_developer_username": "$http_x_developer_username",'
                                     '"remote_addr": "$remote_addr",'
                                     '"method": "$request_method",'
                                     '"request": "$request_uri",'
                                     '"schema": "$scheme",'
                                     '"request_time": "$request_time",'
                                     '"host": "$host",'
                                     '"uri": "$uri",'
                                     '"user_agent": "$http_user_agent",'
                                     '"status": "$status"'
                                  '},'
                                  '"proxy": {'
                                     '"host": "$proxy_host"'
                                  '},'
                                  '"upstream": {'
                                     '"response_time": "$upstream_response_time sec",'
                                     '"cache_status": "$upstream_cache_status"'
                                   '}'
                                 '}';

   #  access_log  /var/log/nginx/access.log  main;
#    access_log   /var/log/nginx/access.log json_log_format;
    access_log off;

    sendfile            on;
    sendfile_max_chunk 512k;
    # directio 4m;
    # directio_alignment 512;
    tcp_nopush          on;
    tcp_nodelay         on;

    reset_timedout_connection on;

    keepalive_requests 100000;
    types_hash_max_size 2048;

    # reduce the data that needs to be sent over network -- for testing environment
    gzip on;
    # gzip_static on;
    gzip_min_length 10240;
    gzip_comp_level 1;
    gzip_vary on;
    gzip_disable msie6;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types
        text/css
        text/javascript
        text/xml
        text/plain
        text/x-component
        application/javascript
        application/x-javascript
        application/json
        application/xml
        application/rss+xml
        application/atom+xml
        font/truetype
        font/opentype
        application/vnd.ms-fontobject
        image/svg+xml;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.


    proxy_cache_path /opt/nginx/cache levels=1:2 keys_zone=api-cache:3000m max_size=100g inactive=43200m use_temp_path=off;
    proxy_temp_path /opt/nginx/cache/other;

    include /etc/nginx/conf.d/ssl.conf;
}` 

My ssl.confg looks like below

 server {

    server_name  _;
    root         /usr/share/nginx/html;

    listen       443 ssl http2 default_server;
    listen       [::]:443 ssl;

    ssl_certificate     "/etc/private/ssl/cert.pem";
    ssl_certificate_key "/etc/private/ssl/key.pem";
    # ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    keepalive_timeout   100;


    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

    location /health {
       default_type application/json;
       return 200 '{"status":"UP"}';
    }
    location /nginx-status {
        stub_status;
    }

    location /trellotest {
        proxy_cache_bypass $http_no_cache_purge $arg_nocache;
        proxy_cache_methods GET POST;
        add_header Cache-Control "public";
        proxy_cache api-cache;
        proxy_cache_valid 200 40320m;
        add_header X-Cache $upstream_cache_status;
        add_header X-Time $request_time;
        proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
        proxy_pass https://mytrelloapp;
    }
}

If possible, Anyone could you please advise me if we have anyway to improve the above configurations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文