1

我正在尝试让 Basic Authentification 与 Rails 3.2 nginx 和 Unicorn 一起使用

该配置适用于托管我的网站。我在控制器中使用了 Rails 基本身份验证,但在测试时遇到了很多问题。.htpasswd 文件也可以工作,我可以限制对静态站点的访问。

在我试过的位置配置中

location /
location /home/deployer/apps/rails/current/public
location /home/deployer/apps/rails/current/

有任何想法吗?

这是我的配置:

upstream unicorn {
  server unix:/tmp/unicorn.blog.sock fail_timeout=0;
}

server {
  listen 80 default deferred;
  server_name railsserver;
  root /home/deployer/apps/rails/current/public;

  location / {
    auth_basic "Restricted";
    auth_basic_user_file /var/www/prototyp/.htpasswd;
  }

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

我现在用这个配置试过了,但它不起作用

server {
  listen 80 default deferred;
  server_name rails.com;
  root /home/deployer/apps/rails/current/public;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri /;

  location / {
    auth_basic "Restricted";
    auth_basic_user_file /var/www/prototyp/.htpasswd;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}
4

1 回答 1

2

Do it like this to get it to work:

location / {
    auth_basic "Restricted";
    auth_basic_user_file /var/www/prototyp/.htpasswd;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
}

You don't need the @unicorn location

于 2013-05-29T17:59:54.930 回答