# 用 nginx 做代理服务器很慢，但在 docker 里用 nginx 就非常快！为什么？

**URL:** <https://meta.discourse.org/t/using-nginx-as-proxy-server-is-very-slow-but-it-is-very-fast-if-using-nginx-in-docker-why/168972>\
**Category:** Self-hosting\
**Created:** [2020年十一月2日 08:44 UTC](https://meta.discourse.org/t/using-nginx-as-proxy-server-is-very-slow-but-it-is-very-fast-if-using-nginx-in-docker-why/168972 "2020-11-02T08:44:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![cosoc](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cosoc/32/198738_2.png) [@cosoc](https://meta.discourse.org/u/cosoc)\
**Post date:** [2020年十一月2日 08:44 UTC](https://meta.discourse.org/t/using-nginx-as-proxy-server-is-very-slow-but-it-is-very-fast-if-using-nginx-in-docker-why/168972/1 "2020-11-02T08:44:35Z")

</div>

使用 nginx 作为代理服务器时速度非常慢，但在 Docker 中使用 nginx 时却非常快！这是为什么？

app.yml

```plaintext
templates:
  #- "templates/postgres.template.yml"
  - "templates/redis.template.yml"
  - "templates/web.template.yml"
  - "templates/sshd.template.yml"
  - "templates/web.ratelimited.template.yml"
## 如果你想要添加 Lets Encrypt (https)，请取消注释下面这两行
  #- "templates/web.ssl.template.yml"
  #- "templates/web.letsencrypt.ssl.template.yml"
  - "templates/web.socketed.template.yml"

expose:
  #- "8080:80" # http
  #- "444:443" # https

params:
  db_default_text_search_config: "pg_catalog.english"

  ## 将 db_shared_buffers 设置为总内存的 25% 最大值。
  ## 将根据检测到的 RAM 由 bootstrap 自动设置，你也可以手动覆盖
  db_shared_buffers: "780MB"

  ## 可以提高排序性能，但会增加每个连接的内存使用量
  # db_work_mem: "512MB"

  ## 此容器应使用哪个 Git 修订版本？（默认：tests-passed）
  version: stable

env:
  # 数据库配置
  DISCOURSE_DB_USERNAME: ****
  DISCOURSE_DB_PASSWORD: ***
  DISCOURSE_DB_HOST: ***
  DISCOURSE_DB_NAME: ***
  DISCOURSE_DB_PORT: ***
  DISCOURSE_DB_BACKUP_PORT: ***

  LANG: zh_CN.UTF-8
  DISCOURSE_DEFAULT_LOCALE: zh_CN

  ## 支持多少个并发 Web 请求？取决于内存和 CPU 核心数。
  ## 将根据检测到的 CPU 由 bootstrap 自动设置，你也可以手动覆盖
  UNICORN_WORKERS: 2

```

nginx 配置

```plaintext
server {
    listen 80; listen [::]:80;
    server_name my.web.org;

    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2; listen [::]:443 ssl http2;
    server_name minetest.cosoc.org;

    ssl_certificate /var/discourse/shared/standalone/ssl/my.web.org.cer;
    ssl_certificate_key /var/discourse/shared/standalone/ssl/my.web.org.key;
    ssl_session_tickets off;
    http2_idle_timeout 5m; # 从默认的 3m 提升至 5m

location / {
        proxy_pass http://unix:/var/discourse/shared/standalone/nginx.http.sock;
        proxy_set_header Host $http_host;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Real-IP $remote_addr;
    }

```

为什么 nginx 表现得如此缓慢？？？但当你不使用它时却很快！！
