Score:0

Nginx www does not work

pt flag

I really do not know why my nginx configuration does not work for www.

My configuration is:

server {
    listen 80;
    server_name postimg.cz www.postimg.cz;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443;

    server_name postimg.cz;

    # SSL Configuration
    ssl_certificate /etc/letsencrypt/live/postimg.cz/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/postimg.cz/privkey.pem;
    ssl_session_cache shared:SSL:10m;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GC>
    ssl_prefer_server_ciphers on;

    # See https://hstspreload.org/ before uncommenting the line below.
    # add_header Strict-Transport-Security "max-age=15768000; preload;";
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Robots-Tag none;
    add_header Content-Security-Policy "frame-ancestors 'self'";
    add_header X-Frame-Options DENY;
    add_header Referrer-Policy same-origin;

    root /var/www/postimg.cz;

    # Disable access to sensitive application files
    location ~* (app|content|lib)/.*\.(po|php|lock|sql)$ {
        return 404;
    }
    location ~* composer\.json|composer\.lock|.gitignore$ {
        return 404;
    }
    location ~* /\.ht {
        return 404;
    }

    # Image not found replacement
    location ~* \.(jpe?g|png|gif|webp)$ {
        log_not_found off;
        error_page 404 /content/images/system/default/404.gif;
    }

    # CORS header (avoids font rendering issues)
    location ~* \.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$ {
        add_header Access-Control-Allow-Origin "*";
    }

    # PHP front controller
    location / {
        index index.php;
        try_files $uri $uri/ /index.php$is_args$query_string;
    }

    # Single PHP-entrypoint (disables direct access to .php files)
    location ~* \.php$  {
        internal;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }
}

But when I go to http://www.postimg.cz it won't redirect to https://postimg.cz why is that? Can you help me with it?

Server: Ubuntu Server 20.04

EDIT // Tried also this, does not work either:

server {
    listen 80;
    server_name www.postimg.cz postimg.cz;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    server_name www.postimg.cz;
    ssl_certificate /etc/letsencrypt/live/www.postimg.cz/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.postimg.cz/privkey.pem;
    return 301 https://postimg.cz$request_uri;
}

server {
    listen 443 ssl;

    server_name postimg.cz;
tilleyc avatar
us flag
Have you tried modifying `return 301 https://$server_name$request_uri;` to `return 301 https://postimg.cz$request_uri;` ? It seems the redirect will take whatever server name is given, but you don’t have https server with that same name.
Bazim avatar
pt flag
Thank you. I totally missed it. It wans't because of that, but this will help too I guess.
Score:0
pt flag

The problem was in the domain hosting provider. Because I forget to adress www to my server IP. I set DNS only without the www. I am just stupid.

djdomi avatar
za flag
remind to accept the answer. Thanks.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.