mardi 28 mai 2019

Forcing www and https redirect on NGINX as backend of Google cloud Load Balancer using $http_x_forwarded_proto

I have a NGINX backend instance for the google cloud platform load balancer. I have and have successfully set up the https redirect. I want to force from non-www to www. For the most part all but one instance redirects to a www url. to Summarize:

https://ampkart.com not successful and stays as https://ampkart.com

I have tried multiple edits to the redirect in the default.conf file to force it but to no avail. I know it probably has something to 'http' in the bracket and it is only affecting the http urls to redirect to www but the https urls are not affected. Can someone please help me modify this code for the redirect to work? Here is my default.conf file for my NGINX server.

 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;
          }
          }

I would like to be able to redirect https://ampkartcom to https://www.ampkart.com




Aucun commentaire:

Enregistrer un commentaire