Nginx redirection from www to non-www

Viewed 22

I have an issue with redirecting from www to non-www.

My expected behaviour: all requests on port 80 and 443 should be redirected to non-www.

I also have a subdomain dev.example.com requests on this subdomain should also be redirected to non-www.

All is working fine with my current configuration except one thing: If I request http://example.com then i'll be redirected to https://dev.example.com and i cannot find the reason for that.

Can anyone tell me what i did wrong?

I have 3 conf files in my nginx sites-enabled directory linked:

first :

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name example.com www.* dev.*;
    return 301 https://$host$request_uri;
}
server {
    listen 443 default_server ssl;
    listen [::]:443 ssl;
    server_name example.com;
    root /var/www/my-site/public;

    # Path of the SSL certificate
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    # Use the file generated by certbot command.
    include /etc/letsencrypt/options-ssl-nginx.conf;
    # Define the path of the dhparam.pem file.
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    disable_symlinks off;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php index.html;

    charset utf-8;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

second:

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name dev.example.com;
    root /var/www/my-dev-site/public;

    # Path of the SSL certificate
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    # Use the file generated by certbot command.
    include /etc/letsencrypt/options-ssl-nginx.conf;
    # Define the path of the dhparam.pem file.
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    disable_symlinks off;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php index.html;

    charset utf-8;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

third(for mailserver):

server {
    if ($host = autoconfig.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = autodiscover.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = mail.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

  listen 80;
  listen [::]:80;
  server_name mail.example.com autodiscover.* autoconfig.*;
  return 301 https://$host$request_uri;
}
server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name mail.example.com autodiscover.* autoconfig.*;

  # Path of the SSL certificate
    ssl_certificate /etc/letsencrypt/live/steamangel.de/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/steamangel.de/privkey.pem; # managed by Certbot
  # Use the file generated by certbot command.
  include /etc/letsencrypt/options-ssl-nginx.conf;
  # Define the path of the dhparam.pem file.
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;


  location /Microsoft-Server-ActiveSync {
    proxy_pass http://127.0.0.1:8080/Microsoft-Server-ActiveSync;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_connect_timeout 75;
    proxy_send_timeout 3650;
    proxy_read_timeout 3650;
    proxy_buffers 64 256k;
    client_body_buffer_size 512k;
    client_max_body_size 0;
  }

  location / {
    proxy_pass http://127.0.0.1:8080/;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    client_max_body_size 0;
  }
}
0 Answers
Related