无法使用Nginx反向代理连接Rails PUMA服务器

时间:2018-12-08 21:56:04

标签: ruby-on-rails docker nginx docker-compose puma

您好,我正在尝试使用nginx和docker container rails应用程序设置反向代理,可以正确提供公共静态文件,但无法访问我的应用程序。 nginx错误日志显示:

2018/12/08 16:46:45 [error] 4093#4093: *350 could not find named location "@puma", client: xx.xxx.xxx.xx, server: my.app, request: "GET /en/users/sign_in HTTP/2.0",host: "my.app", referrer: "https://my.app/"

我的nginx配置是这样的:

upstream puma {
  server 0.0.0.0:3000;
}

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name my.app;
  root /var/www/myapp/public;

  # SSL
  ssl_certificate /etc/letsencrypt/live/my.app/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/my.app/privkey.pem;
  ssl_trusted_certificate /etc/letsencrypt/live/my.app/fullchain.pem;
  include snippets/letsencrypt.conf;
  include snippets/ssl.conf;

  # reverse proxy
  location / {

    proxy_pass http://puma; # => http://0.0.0.0:3000
    proxy_set_header Host $http_host; # => $host
    proxy_set_header X-Forwarded-Proto $scheme; # => "https"
    proxy_set_header X-Forwarded-Host $host; # => 0.0.0.0
    proxy_set_header X-Forwarded-Port $server_port; # => 3000

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";    
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_cache_bypass $http_upgrade;

    # index fallback
    try_files $uri $uri/ /index.html;
  }

  # . files
  location ~ /\. {
    deny all;
  }

  # assets, media
  location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?)$ {
    expires 7d;
    access_log off;
  }

  # svg, fonts
  location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff2?)$ {
    add_header Access-Control-Allow-Origin "*";
    expires 7d;
    access_log off;
  }

  # gzip
  gzip on;
  gzip_vary on;
  gzip_proxied any;
  gzip_comp_level 6;
  gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/atom+xml image/svg+xml;

  # error pages
  error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    root   /var/www/myapp/public;
  }

  error_page 404 /404.html;
    location = /404.html {
    root /var/www/myapp/public;
  }
}

# subdomains redirect
server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name _ *.my.app;

  # SSL
  ssl_certificate /etc/letsencrypt/live/my.app/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/my.app/privkey.pem;
  ssl_trusted_certificate /etc/letsencrypt/live/my.app/chain.pem;
  include snippets/letsencrypt.conf;
  include snippets/ssl.conf;
  return 301 https://my.app$request_uri;
}

# HTTP redirect
server {
  listen 80;
  listen [::]:80;
  server_name _ .my.app my.app;
  include snippets/letsencrypt.conf;
  return 301 https://my.app$request_uri;
}

rails容器通过docker-compose进行以下设置:

version: '3.2'

services:
  web:
    command: rails server -p '3000' -b '0.0.0.0' -e production
    ports:
      - '3000:3000'

我也曾尝试过unix socket to connect puma and nginx并没有成功

0 个答案:

没有答案