Nginx的简单反向代理

Nginx的简单反向代理

本文介绍了Nginx的简单反向代理(等效于Apache)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Apache,我可以使反向代理与此VirtualHost配置一起使用.我已执行 nanoc view -p 8080 将8080端口用于nanoc Web应用程序.

With Apache, I can make reverse proxy working with this VirtualHost configuration.I have executed nanoc view -p 8080to use 8080 port for nanoc web app.

使用此设置,将 http://scalatra.prosseek 映射到nanoc.

With this setup, http://scalatra.prosseek is mapped to the nanoc.

<VirtualHost *:80>
    ProxyPreserveHost On
    ServerName scalatra.prosseek
    ProxyPass  /excluded !
    ProxyPass / http://127.0.0.1:8080/
    ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>

我需要与Nginx进行相同的设置,但要经过一些尝试和错误,我才能使其在此配置下正常工作.

I need to have the same setup with Nginx, with some trial and error, I could make it work with this configuration.

upstream aha { # ??? (1)
        server 127.0.0.1:8080;
        keepalive 8;
}

# the nginx server instance
server {
        listen 0.0.0.0:80;
        server_name scalatra.prosseek;
        access_log /usr/local/etc/nginx/logs/error_prosseek.log;

        location / {
            # ??? (2)
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;

            proxy_pass http://aha/; # ??? (1)
            proxy_redirect off; # ??? (3)
        }
}

它可以工作,但是我不确定这是否是最佳设置.我的问题来了:

It works, but I'm not sure if this is the best setup.Here come my questions:

  • http://scalatra.prosseek 到localhost:8080的设置是否正确:
    • 这些proxy_set_headers的设置正确吗?还是我错过了什么?
    • 对于proxy_pass和上游,只要两个名字相同就可以吗?
    • 我是否需要 proxy_redirect off; ?
    • Is the setup OK for http://scalatra.prosseek to localhost:8080:
      • Are these correct setup of proxy_set_headers? Or did I miss something?
      • For the proxy_pass and upstream, is it just OK as long as two names are the same?
      • Do I need proxy_redirect off;?

      推荐答案

      您的配置看起来很接近.

      Your configuration looks close.

      • 代理头应该没问题.通常Nginx会通过标头,因此当您要修改标头时会使用proxy_set_header-例如,即使客户端不提供标头,也要强制显示Host标头.

      • Proxy headers should be fine. Normally Nginx passes headers through, so proxy_set_header is used when you want to modify those - for example forcing the Host header to be present even if the client does not provide one.

      对于proxy_pass和上游,是的,名称需要匹配.

      For the proxy_pass and upstream, yes the names need to match.

      请考虑将proxy_redirect保留为打开状态(默认).此选项可修改Nginx是否干扰诸如301& amp;的响应.302重定向,包括端口号.关闭它意味着您的upsteam应用程序必须负责在任何重定向响应中传递正确的公共域名和端口.将其保留为默认值意味着,如果您不小心尝试将客户端定向到端口8080,则在某些情况下Nginx会将其更正为端口80.

      Consider leaving proxy_redirect on (default). This option modifies whether Nginx interferes with responses like 301 & 302 redirects including the port number. Turning it off means that your upsteam application must take responsibility for passing the correct public domain name and port in any redirect responses. Leaving it set to default means that if you accidentally try to direct the client to port 8080, Nginx would in some cases correct it to be port 80 instead.

      您还没有在nginx配置中包含/excluded路径.将其添加到

      You also did not include the /excluded path in your nginx config. Add that in with

      location /excluded {
          return 403;
      }
      

      这篇关于Nginx的简单反向代理(等效于Apache)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 00:10