nginx 仅向 unicorn 发送 GET 请求

发布于 2024-12-07 19:34:42 字数 2098 浏览 2 评论 0原文

我正在尝试使用以下虚拟主机运行通过 nginx 代理到 unicorn 的 Rails 3 应用程序。

upstream nginx {
  server unix:/tmp/nginx.socket fail_timeout=0;
}

server {
    listen 80;
    server_name nginx.domain.net;

    rewrite ^(.*) https://nginx.mydomain.net$1 permanent;
}


server {
    listen 443 ssl;
    server_name nginx.mydomain.net;

    root /home/me/nginx.mydomain.net/current/public;
    access_log /home/me/nginx.mydomain.net/shared/log/access.log;
    error_log /home/me/nginx.mydomain.net/shared/log/error.log;

    ssl_certificate /etc/nginx/certs/my_crt_chain.crt;
    ssl_certificate_key /etc/nginx/certs/my_crt_key.key;

    rewrite_log on;

    location / {
        proxy_redirect     off;

        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_cache_methods GET HEAD POST;

        client_max_body_size       10m;
        client_body_buffer_size    128k;

        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;

        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;

        if (!-f $request_filename) {
          proxy_pass http://nginx;
          break;
        }

    }
}

当我启动 unicorn 并尝试发出 POST 请求时,它在 Unicorn 日志中显示为 GET 请求。

I, [2011-09-30T12:38:05.036462 #19364]  INFO -- : unlinking existing socket=/tmp/nginx.socket
I, [2011-09-30T12:38:05.036902 #19364]  INFO -- : listening on addr=/tmp/nginx.socket fd=5
I, [2011-09-30T12:38:05.037435 #19364]  INFO -- : Refreshing Gem list
master process ready
worker=0 ready
worker=1 ready
92.22.194.68 - - [30/Sep/2011 12:38:13] "GET /reset HTTP/1.0" 200 - 0.6486

我是 nginx 的新手,但似乎 POST 请求没有被发送。我找不到在任何地方(nginx 配置、虚拟主机)显式设置 proxy_cache_methods,但我也显式设置它以允许在此虚拟主机中进行 POST:proxy_cache_methods GET HEAD POST; 虚拟主机是否包含此行没有任何区别。

我认为不值得发布 unicorn.rb 配置文件,因为它似乎是 nginx 代理的问题,但如果需要我可以这样做。

I'm trying to run a Rails 3 app proxied through nginx to unicorn, using the following virtual host.

upstream nginx {
  server unix:/tmp/nginx.socket fail_timeout=0;
}

server {
    listen 80;
    server_name nginx.domain.net;

    rewrite ^(.*) https://nginx.mydomain.net$1 permanent;
}


server {
    listen 443 ssl;
    server_name nginx.mydomain.net;

    root /home/me/nginx.mydomain.net/current/public;
    access_log /home/me/nginx.mydomain.net/shared/log/access.log;
    error_log /home/me/nginx.mydomain.net/shared/log/error.log;

    ssl_certificate /etc/nginx/certs/my_crt_chain.crt;
    ssl_certificate_key /etc/nginx/certs/my_crt_key.key;

    rewrite_log on;

    location / {
        proxy_redirect     off;

        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_cache_methods GET HEAD POST;

        client_max_body_size       10m;
        client_body_buffer_size    128k;

        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;

        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;

        if (!-f $request_filename) {
          proxy_pass http://nginx;
          break;
        }

    }
}

When I start unicorn and attempt to make a POST request, it's showing up in the Unicorn logs as a GET request.

I, [2011-09-30T12:38:05.036462 #19364]  INFO -- : unlinking existing socket=/tmp/nginx.socket
I, [2011-09-30T12:38:05.036902 #19364]  INFO -- : listening on addr=/tmp/nginx.socket fd=5
I, [2011-09-30T12:38:05.037435 #19364]  INFO -- : Refreshing Gem list
master process ready
worker=0 ready
worker=1 ready
92.22.194.68 - - [30/Sep/2011 12:38:13] "GET /reset HTTP/1.0" 200 - 0.6486

I'm new to nginx but it seems that somehow the POST requests aren't being sent through. I can't find proxy_cache_methods set explicity anywhere (nginx config, virtual host) but I've also set it explicity to allow POST in this virtual host: proxy_cache_methods GET HEAD POST; Whether the virtual host includes this line or not doesn't make any difference.

I didn't think it was worth posting the unicorn.rb configuration file as it seems to be an issue with nginx's proxying, but I can do if needed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黎歌 2024-12-14 19:34:42

看来这还需要 location 块下的以下内容:

    proxy_set_header X-FORWARDED_PROTO https;

It seems this also needs the following under the location block:

    proxy_set_header X-FORWARDED_PROTO https;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文