Nginx:如果上下文不存在则添加上下文 - 重定向
Nginx: Add context if one doesn't exist - in redirection
要求:
在其中重定向 http://ruby.server.com to https://ruby.server.com/app1/ and don't append anything if the incoming URL has context (ex: http://ruby.server.com/app2/)
设置
我们有以下设置
亚马逊负载均衡器
| http - |
| --> 80(ruby.server.com)
| https - |
nginx是运行 on ruby.server.com at port 80.nginx中没有443.
server {
listen 80;
server_name ruby.server.com;
root /home/ubuntu/ruby/server/public/;
location / {
if ($http_x_forwarded_proto != 'https') {
rewrite ^ https://$host$request_uri? permanent;
}
client_max_body_size 100M;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:4009/;
root /home/ubuntu/ruby/server/public/;
}
if ($http_x_forwarded_proto != 'https') line is used for redirecting http://ruby.server.com to https://ruby.server.com
更多上下文
我们在 rails 服务器中有 2 个 rails 应用程序 运行,/public/app1,/public/app2。默认情况下,我们希望重定向 ruby.server.com 到 app1.
我们可以在 rails 本身内解决上述问题,这涉及到额外的重定向。我们正在尝试查看是否可以在 nginx 层中添加该上下文。
基于@timothy 的注释的解决方案。
server {
listen 80;
server_name ruby.server.com;
root /home/ubuntu/staging/server/public/;
location / {
if ($http_x_forwarded_proto != 'https') {
rewrite ^ https://$host/app1/? permanent;
}
client_max_body_size 100M;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:4009/;
root /home/ubuntu/staging/server/public/;
#auth_basic "Restricted";
#auth_basic_user_file /etc/nginx/.htpasswd;
}
location ^~ /[^\/]+/ {
if ($http_x_forwarded_proto != 'https') {
rewrite ^ https://$host$request_uri? permanent;
}
client_max_body_size 100M;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:4009/;
}
}
server {
listen 80;
server_name ruby.server.com;
root /home/ubuntu/ruby/server/public/;
location / {
# Default location for:
# http://ruby.server.com
# https://ruby.server.com
if ($http_x_forwarded_proto != 'https') {
rewrite ^ https://$host$request_uri? permanent;
}
client_max_body_size 100M;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:4009/;
}
location ^~ /[^\/]+/ {
# Location for:
# http://ruby.server.com/anything/
# https://ruby.server.com/anything/
# Do whatever you need here. :)
client_max_body_size 100M;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:4009/;
}
}
要求:
在其中重定向 http://ruby.server.com to https://ruby.server.com/app1/ and don't append anything if the incoming URL has context (ex: http://ruby.server.com/app2/)
设置
我们有以下设置
亚马逊负载均衡器
| http - |
| --> 80(ruby.server.com)
| https - |
nginx是运行 on ruby.server.com at port 80.nginx中没有443.
server {
listen 80;
server_name ruby.server.com;
root /home/ubuntu/ruby/server/public/;
location / {
if ($http_x_forwarded_proto != 'https') {
rewrite ^ https://$host$request_uri? permanent;
}
client_max_body_size 100M;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:4009/;
root /home/ubuntu/ruby/server/public/;
}
if ($http_x_forwarded_proto != 'https') line is used for redirecting http://ruby.server.com to https://ruby.server.com
更多上下文
我们在 rails 服务器中有 2 个 rails 应用程序 运行,/public/app1,/public/app2。默认情况下,我们希望重定向 ruby.server.com 到 app1.
我们可以在 rails 本身内解决上述问题,这涉及到额外的重定向。我们正在尝试查看是否可以在 nginx 层中添加该上下文。
基于@timothy 的注释的解决方案。
server {
listen 80;
server_name ruby.server.com;
root /home/ubuntu/staging/server/public/;
location / {
if ($http_x_forwarded_proto != 'https') {
rewrite ^ https://$host/app1/? permanent;
}
client_max_body_size 100M;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:4009/;
root /home/ubuntu/staging/server/public/;
#auth_basic "Restricted";
#auth_basic_user_file /etc/nginx/.htpasswd;
}
location ^~ /[^\/]+/ {
if ($http_x_forwarded_proto != 'https') {
rewrite ^ https://$host$request_uri? permanent;
}
client_max_body_size 100M;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:4009/;
}
}
server {
listen 80;
server_name ruby.server.com;
root /home/ubuntu/ruby/server/public/;
location / {
# Default location for:
# http://ruby.server.com
# https://ruby.server.com
if ($http_x_forwarded_proto != 'https') {
rewrite ^ https://$host$request_uri? permanent;
}
client_max_body_size 100M;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:4009/;
}
location ^~ /[^\/]+/ {
# Location for:
# http://ruby.server.com/anything/
# https://ruby.server.com/anything/
# Do whatever you need here. :)
client_max_body_size 100M;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:4009/;
}
}