警告:到目前为止,我只学习了如何使用nginx为自己的域和服务器块提供应用程序。 但我认为是时候深入一点。
为了减轻对多个SSL证书或昂贵的通配符证书的需求,我想从一个nginx server_name提供多个应用程序(例如rails应用程序,php应用程序,node.js应用程序)。 例如rooturl / railsapp rooturl / nodejsapp rooturl / phpshop rooturl / phpblog
我不确定理想的策略。 我已经看到和想到的一些例子:
多个位置规则,这似乎导致各个应用程序configuration要求之间的冲突,例如不同的重写和访问要求
通过后端内部端口隔离应用程序,这可能吗? 每个端口路由到自己的configuration? 所以configuration是孤立的,可以定制到应用程序的要求。
反向代理,我对这是如何工作的小无知,这是我需要研究? 这实际上是2以上? 在线帮助似乎总是代理到另一台服务器,如Apache
什么是有效的方法来隔离从一个单一的域通过子uris服务的应用程序的configuration要求?
Nginx可以做很多事情,包括反向代理,caching和服务内容,但是在大型环境中,单独的function被拆分,使得它们更易于维护或专门用于更好的替代scheme(如大容量https://的stud)。
反向代理只是意味着客户端和实际应用程序之间的东西; 这实际上是一个误导,应该被称为“服务器代理”。
要从一个领域的一个证书服务一切,从这样的事情开始:
(在Ubuntu LTS 12.04上testing)
#proxy_set_header Host $proxy_host; # instead of standard $host proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# note: must disable the built-in # /etc/nginx/sites-enabled/default by removing it (it's a symlink) server { # redirects all http:// requests to https:// # critically, passes the original host the client was trying to connect to. rewrite ^ https://$host$request_uri? permanent; # combined redirect access and error logs for easier correlation error_log '/var/log/nginx/global_redirects'; access_log '/var/log/nginx/global_redirects'; }
# This serves all enabled-locations over ssl only. # If there's no match, it shows the default site. include /etc/nginx/upstreams-enabled/*; # include enabled upstream proxies server { listen 443 ssl; ssl_certificate /etc/nginx/server.crt; ssl_certificate_key /etc/nginx/server.key; keepalive_timeout 70; root /usr/share/nginx/www; index index.html index.htm; access_log '/var/log/nginx/global_ssl'; error_log '/var/log/nginx/global_ssl'; include /etc/nginx/locations-enabled/*; }
# points to hackernews but # it could be http://10.2.4.5:401/app495 instead location ~ ^/bar(/.*)?$ { include proxy_params; include apps/node; proxy_pass http://news.ycombinator.com/$1; access_log '/var/log/nginx/bar'; error_log '/var/log/nginx/bar'; }
location ~ ^/foo(/.*)?$ { include proxy_params; include apps/ruby; proxy_pass http://www.linode.com/$1; access_log '/var/log/nginx/foo'; error_log '/var/log/nginx/foo'; }
upstream news.ycombinator.com { server news.ycombinator.com; }
upstream www.linode.com { server www.linode.com; }
# Place ruby specific directives here
# Place node specific directives here
请记住,这不会重写网页中的url,因为这是由每个应用程序生成的。 相反,每个应用程序应该知道它的外部scheme,主机,端口和url库,并产生适当的链接(大多数真实的应用程序支持这一点)。
参考文献: