0

我有一个用于谷歌云平台负载均衡器的 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;
          }
          }

我希望能够将https://ampkartcom重定向到https://www.ampkart.com

4

1 回答 1

0

解决了。如果您尝试使用 Google 云负载均衡器的 nginx 后端实现强制 https 和 www 或非 www,只需使用下面的寒冷:将其放在 server_name 下方,将 example.com 更改为您的域。

if ($http_host ~* '^example.com'){ 
rewrite ^(.*)$ https://www.example.com$request_uri redirect; } 

这将强制使用 nginx 后端的 https,如果您不想要 www 并且想要强制使用非 www,只需删除“www”。

此外,这里是我的网站 nginx 后端的完整 default.conf,它是启用了 php-fpm 的谷歌云负载均衡器。享受!

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
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 /www;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

    server_name www.example.com;     

if ($http_host ~* '^example.com'){ 
rewrite ^(.*)$ https://www.example.com$request_uri redirect; } 

                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$ {  

                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;

}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny all;
    }

}

# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#   listen 80;
#   listen [::]:80;
#
#   server_name example.com;
#
#   root /var/www/example.com;
#   index index.html;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}
于 2019-05-31T00:12:40.577 回答