Rails应用程序无法在Nginx反向代理下工作

时间:2018-12-04 14:44:32

标签: ruby-on-rails nginx

您好,我试图通过nginx反向代理使其在我的dockerized rails应用程序中工作,nginx可以很好地处理静态文件,但是当我尝试在该应用程序下登录或浏览时,nginx会抛出500错误页面

也许我丢失了某些东西,或者缺少配置使得无法访问myapp

/etc/nginx/nginx.conf

user www-data;
worker_processes auto;
worker_rlimit_nofile 1024;

events {
  multi_accept on;
  worker_connections 1024;
}

http {
  charset utf-8;
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  server_tokens off;
  log_not_found off;
  types_hash_max_size 2048;
  client_max_body_size 16M;

  # MIME
  include mime.types;
  default_type application/octet-stream;

  # logging
  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log warn;

  # load configs
  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*.conf;
}

/etc/nginx/sites-available/my_app.conf

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 needs to be name of service on docker compose to connect with container
    proxy_pass http://0.0.0.0:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    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_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Port $server_port;
    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;
}
  • nginx目前在没有防火墙的主机Ubuntu 18.04上
  • 将容器指向http://0.0.0.0:3000
  • 在彪马中,可以看到浏览器何时点击日志中显示的登录页面

有什么主意吗?谢谢

0 个答案:

没有答案