0

I have hard time making nginx redirection to non-www for subdomain of example.com.

Basically what I want to is:

  1. redirect all http to https 80 to 443
  2. keep api.exmaple.com
  3. keep example.com
  4. keep www.example.com
  5. redirect https://www.subdomain.example.com to https://subdomain.example.com

And below code does not work, and according to nginx www redirect to non-www with subdomain it seems that it should work.

Appreciate all help. Please advise!


server {

    listen   80;
    server_name example.com *.example.com;
    return 301 https://$host$request_uri;
}

server {

    listen 443 ssl;

    server_name api.example.com;
    root /var/www/html.live/laravel/public;
    index index.php index.html index.htm;

    ssl_certificate     /etc/ssl/wild_example.crt;
    ssl_certificate_key /etc/ssl/wild_example.crt;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;

    add_header 'Access-Control-Allow-Origin' "$http_origin" always;
    add_header "Vary" "Origin";
    add_header 'Access-Control-Allow-Methods' 'POST, GET, OPTIONS, DELETE, PUT';
    add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, MBM-User-Token, X-CSRF-TOKEN';
    add_header 'Access-Control-Allow-Credentials' 'true';

    access_log /var/log/nginx/api_access.log;
    error_log /var/log/nginx/api_error.log warn;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php-fpm/php70_www.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}


server {
    listen   443 ssl;
    server_name ~^www\.(?<site_id>.+)\.example\.com$;
    return 301 $scheme://$site_id.example.com$request_uri;
}

# CAKE 443
server {

    listen 443 ssl;
    server_name *.example.com;
    root   /var/www/html.live/app/webroot/;
    index  index.php;

    client_max_body_size 32M;

    ssl_certificate     /etc/ssl/wild_example.crt;
    ssl_certificate_key /etc/ssl/wild_example.crt;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;

    access_log /var/log/nginx/main_access.log main_ext;
    error_log /var/log/nginx/main_error.log warn;

    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'POST, GET, OPTIONS, DELETE, PUT';
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

    location / {
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
        }
        try_files $uri $uri/ /index.php?$args;
    }

    if (!-e $request_filename) {
      rewrite ^(.+)$ /index.php?q=$1 last;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php-fpm/php56_www.sock;
        fastcgi_index   index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param origin_is $host;
        include /etc/nginx/fastcgi_params;
    }
}

server {

    listen 443 ssl;
    server_name example.com;
    root   /var/www/html.live/app/webroot/;
    index  index.php;

    client_max_body_size 32M;

    ssl_certificate     /etc/ssl/wild_example.crt;
    ssl_certificate_key /etc/ssl/wild_example.crt;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;

    access_log /var/log/nginx/main_access.log main_ext;
    error_log /var/log/nginx/main_error.log warn;

    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'POST, GET, OPTIONS, DELETE, PUT';
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

    location / {
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
        }
        try_files $uri $uri/ /index.php?$args;
    }

    if (!-e $request_filename) {
            rewrite ^(.+)$ /index.php?q=$1 last;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php-fpm/php56_www.sock;
        fastcgi_index   index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param origin_is $host;
        include /etc/nginx/fastcgi_params;
    }
}
Rafał Łyczkowski
  • 995
  • 2
  • 11
  • 27
  • You can't use `*.example.com` as it will match before your regular expression server name. Take a look at [this document](http://nginx.org/en/docs/http/server_names.html). – Richard Smith Nov 16 '18 at 20:47
  • https://serverfault.com/questions/67316/in-nginx-how-can-i-rewrite-all-http-requests-to-https-while-maintaining-sub-dom – Sheppy Nov 18 '18 at 10:39
  • Possible duplicate of [Nginx no-www to www and www to no-www](https://stackoverflow.com/questions/7947030/nginx-no-www-to-www-and-www-to-no-www) – Sheppy Nov 18 '18 at 10:40

0 Answers0