我尝试使用 Thin 应用程序服务器,但遇到了一个问题。
当 nginx 代理使用
proxy_pass http://my_app_upstream;
向 Thin(或 Unicorn)发出请求时,应用程序会收到 nginx (http://my_app_upstream
) 发送的修改后的 URL。
我想要的是不加修改地传递原始 URL 和来自客户端的原始请求,因为应用程序严重依赖它。
nginx' doc 说:
如果需要传输URI 未处理的表单 then 指令 proxy_pass 应该在没有 URI 的情况下使用 部分。
但我不明白如何准确配置它,因为相关示例实际上使用的是 URI:
location /some/path/ {
proxy_pass http://127.0.0.1;
}
那么您能帮我弄清楚如何从客户端保留原始请求 URL 吗?
location / {
proxy_pass http://BACKENDIP/;
include /etc/nginx/proxy.conf;
}
然后在 proxy.conf 中
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
根据 nginx 1.8.x 的 @iwein 评论进行更新(参见 https://stackoverflow.com/a/57350306/548473):
isoproxy_set_header Host $host:$server_port;
使用
proxy_set_header Host $http_host
所以你只需要做:
proxy_set_header Host $http_host;
try_files
,这样保留了对后端的请求:
location / {
proxy_pass http://127.0.0.1:8080$request_uri;
}
absoluteURI
和标头中的
Host
:
server {
listen 35005;
location / {
rewrite ^(.*)$ "://$http_host$uri$is_args$args";
rewrite ^(.*)$ "http$uri$is_args$args" break;
proxy_set_header Host $host;
proxy_pass https://deploy.org.local:35005;
}
}
在这里找到:
https://opensysnotes.wordpress.com/2016/11/17/nginx-proxy_pass-with-absolute-url/
server {
server_name dashboards.etilize.com;
location / {
proxy_pass http://demo.etilize.com/dashboards/;
proxy_set_header Host $http_host;
}}
$http_host 将在标头中设置与请求相同的 URL
.
####################################################
upstream auth {
server 127.0.0.1:9011 weight=1 fail_timeout=300s;
keepalive 16;
}
在我的 443 服务器块内
if (-d $request_filename) {
rewrite [^/]$ $scheme://$http_host$uri/ permanent;
}
location /auth {
proxy_pass http://$http_host:9011;
proxy_set_header Origin http://$host;
proxy_set_header Host $http_host:9011;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_http_version 1.1;
}
conf底部
#####################################################################
# #
# Proxies for all the Other servers on other ports upstream #
# #
#####################################################################
#######################
# Fusion #
#######################
server {
listen 9001 ssl;
############# Lock it down ################
# SSL certificate locations
ssl_certificate /etc/letsencrypt/live/allineed.app/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/allineed.app/privkey.pem;
# Exclusions
include snippets/exclusions.conf;
# Security
include snippets/security.conf;
include snippets/ssl.conf;
# Fastcgi cache rules
include snippets/fastcgi-cache.conf;
include snippets/limits.conf;
include snippets/nginx-cloudflare.conf;
########### Location upstream ##############
location ~ / {
proxy_pass http://auth;
proxy_set_header Origin http://$host;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_http_version 1.1;
}
if (-d $request_filename) {
rewrite [^/]$ $scheme://$http_host$uri/ permanent;
}
}