我正在运行 nginx 反向代理,以 apache 作为后端服务器。我有多个应用程序在运行,没有任何问题,但一些 php-js 网站无法正确重定向。当我向example.com/admin它应该重定向到example.com/admin/但我得到的是重定向到示例.com:内部端口/行政/。
以下是一些有用的信息:
Nginx 配置:
server { server_name example.com; root /var/www/html/; index index.php index.html index.htm; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header Host $host; proxy_pass http://localhost:8080; proxy_read_timeout 600s; } location ~ /\.ht { deny all; } listen 80; }
Apache配置:
<VirtualHost localhost:8080> DocumentRoot /var/www/html/example.com/ ServerName localhost:8080 LogLevel debug ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
从后端服务器请求时的Http标头:
$ curl localhost:8080/admin -v
GET /admin HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.58.0
Accept: */*
HTTP/1.1 301 Moved Permanently
Date: Wed, 06 Jul 2022 17:59:37 GMT
Location: http://localhost:8080/admin/
Content-Length: 313
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://localhost:8080/admin/">here</a>.</p>
<hr>
</body></html>
- 请求虚拟主机时的http headers:
$ curl example.com/admin -v
> GET /admin HTTP/1.1
> Host: example.com
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.14.0 (Ubuntu)
< Date: Thu, 07 Jul 2022 09:13:15 GMT
< Content-Type: text/html; charset=iso-8859-1
< Content-Length: 325
< Connection: keep-alive
< Location: http://example.com:8080/admin/
<
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://example.com:8080/admin/">here</a>.</p>
<hr>
答案1
我刚刚在服务器块中的 nginx 配置中添加了以下行:
rewrite /admin$ $scheme://$host$uri/ permanent;