can't connect rails puma server with nginx reverse proxy

谁说胖子不能爱 提交于 2019-12-25 01:35:07

问题


Hello I am trying to setup a reverse proxy with nginx and docker container rails app, public static files are served correctly but can not access to my app. the nginx error log says:

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/"

my nginx config is this:

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;
}

the rails container start thru docker-compose with this setup:

version: '3.2'

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

I have tried also with unix socket to connect puma and nginx without success

来源:https://stackoverflow.com/questions/53687350/cant-connect-rails-puma-server-with-nginx-reverse-proxy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!