简体   繁体   English

Nginx不渲染彪马应用

[英]Nginx not rendering puma application

I'm trying to serve my rails application using Puma and Nginx. 我正在尝试使用Puma和Nginx服务我的rails应用程序。 When ever I got to the page it renders the default nginx page. 每当我进入该页面时,它将呈现默认的nginx页面。 I've tried with two different configurations. 我尝试了两种不同的配置。 This first fails with "upstream" directive is not allowed here. 第一次失败, "upstream" directive is not allowed here.使用"upstream" directive is not allowed here. The second warns Starting nginx: nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored 第二个警告Starting nginx: nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored

(1) overwrite nginx.conf (1)覆盖nginx.conf

nginx.conf nginx.conf

upstream puma {
  server unix:///home/deploy/apps/exelon-api/shared/tmp/sockets/rails-api-puma.sock;
}
server {
  listen 80 default_server deferred;
  # server_name example.com;
  root /home/deploy/apps/rails-api/current/public;
  access_log /home/deploy/apps/rails-api/current/log/nginx.access.log;
  error_log /home/deploy/apps/rails-api/current/log/nginx.error.log info;
  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }
  try_files $uri/index.html $uri @puma;
  location @puma {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://puma;
  }
  error_page 500 502 503 504 /500.html;
  client_max_body_size 10M;
  keepalive_timeout 10;
}

(2) using sites enabled (2)使用启用的网站

nginx.conf nginx.conf

user nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;
events {
  worker_connections  1024;
}
http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;
  access_log    /var/log/nginx/access.log;
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_requests 100;
  keepalive_timeout  65;
  gzip  on;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_proxied any;
  gzip_vary off;
  gzip_types text/plain text/css application/x-javascript text/xml application/xml application/rss+xml application/atom+xml text/javascript application/javascript application/json text/mathml;
  gzip_min_length  1000;
  gzip_disable     "MSIE [1-6]\.";
  variables_hash_max_size 1024;
  variables_hash_bucket_size 64;
  server_names_hash_bucket_size 64;
  types_hash_max_size 2048;
  types_hash_bucket_size 64;
  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
}

sites-enable/rails-api_production 网站启用/ Rails的api_production

upstream puma_rails-api_production { 
  server unix:/home/deploy/apps/rails-api/shared/tmp/sockets/rails-api-puma.sock fail_timeout=0;
}
server {
  listen 80;
  client_max_body_size 4G;
  keepalive_timeout 10;
  error_page 500 502 504 /500.html;
  error_page 503 @503;
  server_name localhost rails-api.local;
  root /home/deploy/apps/rails-api/current/public;
  try_files $uri/index.html $uri @puma_rails-api_production;
  location @puma_rails-api_production {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://puma_rails-api_production;
    # limit_req zone=one;
    access_log /home/deploy/apps/rails-api/shared/log/nginx.access.log;
    error_log /home/deploy/apps/rails-api/shared/log/nginx.error.log;
  }
  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }
  location = /50x.html {
    root html;
  }
  location = /404.html {
    root html;
  }
  location @503 {
    error_page 405 = /system/maintenance.html;
    if (-f $document_root/system/maintenance.html) {
      rewrite ^(.*)$ /system/maintenance.html break;
    }
    rewrite ^(.*)$ /503.html break;
  }
  if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
    return 405;
  }
  if (-f $document_root/system/maintenance.html) {
    return 503;
  }
  location ~ \.(php|html)$ {
    return 405;
  }
}

Second error practically says that you have two server sections defined with listen 80 (which practically means 0.0.0.0:80 where 0.0.0.0 is equal to "any address") and localhost in server_name . 第二个错误实际上表示您在server_name定义了两个server部分,它们分别定义为listen 80 (实际上意味着0.0.0.0:80 ,其中0.0.0.0等于“任何地址”)和localhost

So, action you have to take depends on what you want to achieve: 因此,您必须采取的行动取决于您要实现的目标:

Have some other app as default 将其他一些应用程序默认设置为

If you want to have your app to be accessible alongside some other apps you have (or want to have in future) on server by some hostname , let's say example.com , you have to simply remove localhost from server_name in the entry you added. 如果要让您的应用程序可以通过主机名 (例如example.com )与您在服务器上拥有(或将来希望拥有的其他一些应用程序)一起访问(例如example.com ,则只需在添加的条目中从server_name中删除localhost

In this case, you will have to access your app by one of the names you have specified for it in server_name ( example.com ), neither localhost nor server IP. 在这种情况下,您将必须使用在server_nameexample.com )中为其指定的名称之一访问应用程序,而不是localhost或服务器IP。

Note that if you still have to make sure domain you've specified resolves to the server address. 请注意 ,如果仍然必须确保您指定的域解析为服务器地址。

If it is on your own local machine and you want to be able to access your app for development purposes (testing how it works with nginx, for example) you can add row 127.0.0.1 name.here (for example, 127.0.0.1 example.com ). 如果它在您自己的本地计算机上并且您希望能够出于开发目的访问您的应用程序(例如,测试其与nginx的配合使用),则可以在127.0.0.1 name.here添加行127.0.0.1 name.here (例如127.0.0.1 example.com )。

If it is on some server you own and you want others to be able to access app by the same domain, you have to buy/register it (if it has not been registered by someone else yet, it happens) and create the A DNS record pointing to your app server IP. 如果它在您拥有的某些服务器上,并且您希望其他人能够通过同一域访问应用程序,则必须购买/注册它(如果它尚未被其他人注册,则会发生)并创建A DNS记录指向您的应用服务器IP。

Make your app default 将您的应用设为默认

If you want this app to be default on the server , you can: 如果您希望该应用程序在服务器上成为默认设置,则可以:

  1. Find other server sections defined with listen 80 default; 查找使用listen 80 default;定义的其他serverlisten 80 default; (so, if they don't have default or listen any other port like 8080 or 443 you can safely leave them) and remove the default from there - there can be only one default server section per port and IP combination. (因此,如果它们没有default端口,或者监听8080或443之类的其他端口,则可以放心保留它们)并从那里删除default端口-每个端口和IP组合只能有一个默认服务器部分。

  2. Change listen 80; 改变listen 80; in your server section to listen 80 default; 在您的服务器部分中listen 80 default; . This will tell nginx that you want this server section to handle all requests not catched by other section. 这将告诉nginx您希望该服务器部分处理其他部分未捕获的所有请求。

This will allow you to access app by IP and point any domain to it without specifying it in server_name section. 这将允许您通过IP访问应用程序并将任何域指向该应用程序,而无需在server_name部分中进行指定。 You still need to register or buy domain to be able to point it to app. 您仍然需要注册或购买域才能将其指向应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM