Nginx: redirect custom domain http requests to https

Junji Zhi

We want to redirect something.custom.com http requests to https, leaving the other requests unchanged. Tried something like below, but it didn't work:

server {
  listen 80 default_server;
  listen [::]:80 default_server ipv6only=on;

  server_name _;

  if ($host ~ ^(.*?)\.custom\.com) {
    return 301 https://$host$request_uri;
  }

  root /www/example/public;
}

server {
  listen 443 default_server ssl;
  listen [::]:443 default_server ipv6only=on ssl;

  server_name _;

  root /www/example/public;
}

Is there something wrong with my setup?

Keenan Lawrence

As stated in the comments, the configuration below should work:

server {
    listen 80;
    listen [::]:80 ipv6only=on;

    server_name something.custom.com;
    return 301 https://something.custom.com$request_uri;
}

server {
    listen 80 default_server;
    server_name _;

    #Rest of your config here
}

server {
    listen 443 default_server ssl;
    listen [::]:443 default_server ipv6only=on ssl;

    server_name _;

    root /www/example/public;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Redirect Elastic Beanstalk HTTP requests to HTTPS with nginx

Nginx with HTTP and HTTPS but no redirect

NGINX redirect http to https

Nginx - Redirect HTTP to HTTPS

advanced nginx http to https redirect on custom and different ports

How to redirect all HTTPS requests to HTTP requests?

nginx - Disable http to https redirect?

Redirect HTTP to HTTPS (Middleware redirect vs Nginx)

Nginx: How to redirect each http:port requests to HTTPS:port in the below config?

How do I redirect all HTTP requests to HTTPS in ELB without Apache and Nginx

htaccess redirect domain to https, subdomain with parameter to http

Redirect http to https and subdomain to root domain

Redirect http to https (excluding sub domain)

http://domain to https://www in one redirect

Redirect http requests to https in wildfly 10

How to redirect all HTTP requests to HTTPS

Redirect all http requests (including subdomains) to https

Recommended way to to redirect HTTP requests to HTTPS

NGINX redirect old https domain to new non-https

Nginx rewrite HTTP to HTTPS says redirect loop?

Redirect Http to Https in Elastic BeansTalk, Tomcat, Nginx

nginx redirect all http to https with exceptions

Nginx loses POST variable with http -> https redirect

Redirect http to https nginx in docker container

Redirect several HTTP servers to HTTPS in nginx

Redirect Nginx HTTP/HTTPS www to non www

kubernetes nginx ingress fails to redirect HTTP to HTTPS

My redirect from http to https in Nginx not working

Nginx redirect http://www and naked http/https to https://www