我在 Google Cloud 中设置了 nginx 服务器。服务器正在通过 gcloud 指向从主机域访问。
像这样:
https://server.com/test/ points to instance group with one of instances with ip http://39.99.99.99.55/project/
一切都很顺利,除非你输入
https://server.com/test/folder without the leading slash
当您执行此操作时,主机服务器将重定向到
https://server.com/project/folder/
主机域上未分配的文件夹。
但如果你正确输入:
https://server.com/test/folder/
它可以正常解析 IP 服务器并获取结果。
因此,带有 nginx 的服务器会尝试找到该文件夹并执行重定向。
但是,我不希望它使用服务器的相对路径进行重定向,我希望它具有这样的负载平衡主机路径:
https://server.com/test/folder
应该重定向到https://server.com/test/folder/
不https://server.com/project/folder/
这是我的 nginx 配置文件:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /home/.www;
# Add index.php to the list if you are using PHP
index index.php index.html;
server_name _;
# autoindex on;
# serve static files directly
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
#expires max;
}
error_log /home/.log/xerror.log;
access_log /home/.log/xaccess.log compression;
# include project
location ^~ /project {
alias /home/.www/project/public;
try_files $uri $uri/ @project;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_read_timeout 180;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
location @project {
rewrite /project/(.*)$ /project/index.php last;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$args;
}
# pass PHP scripts to FastCGI server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_read_timeout 180;
# With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
}
如何让服务器在找到文件夹后不进行那样的重定向?
编辑1 @Ivan Shatsky
要求从请求中获取curl -v server.com/test/folder
输出,如下所示:
* Trying 35.***.160.34:443...
* TCP_NODELAY set
* Connected to server.com (35.***.160.34) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/certs/ca-certificates.crt
CApath: /etc/ssl/certs
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use h2
* Server certificate:
* subject: CN=server.com
* start date: Apr 25 19:19:01 2022 GMT
* expire date: Jul 24 19:19:00 2022 GMT
* subjectAltName: host "server.com" matched cert's "server.com"
* issuer: C=US; O=Google Trust Services LLC; CN=GTS CA 1D4
* SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x556617a0f2f0)
> GET /test/quasar HTTP/2
> Host: server.com
> user-agent: curl/7.68.0
> accept: */*
>
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* old SSL session ID is stale, removing
* Connection state changed (MAX_CONCURRENT_STREAMS == 100)!
< HTTP/2 301
< server: nginx/1.20.2
< date: Mon, 23 May 2022 21:04:50 GMT
< content-type: text/html
< content-length: 169
< location: http://server.com/project/folder/
< via: 1.1 google
< alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
<
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.20.2</center>
</body>
</html>
* Connection #0 to host server.com left intact
因此看起来它确实到达了我的 nginx 服务器并且返回 301 重定向但在其内部有 /project/folder/ 文件夹的文件夹下这对于该服务器是正确的,但是一旦它传播到主机服务器就变得不正确了,而这应该是server.com/test/folder/
而不是server.com/project/folder/
。