我有一个用于谷歌云平台负载均衡器的 NGINX 后端实例。我已经并且已经成功设置了 https 重定向。我想强制从非 www 到 www。在大多数情况下,除了一个实例之外的所有实例都重定向到 www url。总结:
https://ampkart.com不成功并保持为https://ampkart.com
我已尝试对 default.conf 文件中的重定向进行多次编辑以强制执行它,但无济于事。我知道括号中可能有一些“http”,它只会影响 http url 以重定向到 www,但 https url 不受影响。有人可以帮我修改此代码以使重定向起作用吗?这是我的 NGINX 服务器的 default.conf 文件。
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /storage;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name ampkart.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
error_page 405 =200 $uri;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
if ($http_x_forwarded_proto = 'http'){
rewrite ^/(.*)$ https://www.ampkart.com/$1 permanent;
}
include snippets/fastcgi-php.conf;
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
if ($request_method ~* "(GET|POST|OPTIONS)") {
add_header "Access-Control-Allow-Methods" "GET, POST,
OPTIONS";
add_header "Access-Control-Allow-Credentials" "true";
add_header "Access-Control-Allow-Origin"
"https://www.ampkart.com";
add_header "Access-Control-Expose-Headers" "AMP-
Access-Control-Allow-Source-Origin,Content-Length,Content-Range";
add_header 'Access-Control-Allow-Headers' 'DNT,User-
Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-
Type,Range';
add_header 'AMP-Access-Control-Allow-Source-Origin'
"https://www.ampkart.com";
add_header "AMP-Same-Origin" "true";
add_header "AMP-Redirect-to"
"https://www.ampkart.com$request_uri";
}
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}