Score:0

Nginx server blocks not redirecting

id flag

I am working on a project at https://modernamedia.no/ and I am trying to do a multiple of things

the last one is working. however, the first two are not working. You can test it yourself by going to https://www.modernamedia.no/

I am also struggling reaching my localhost through an API call, but that may be a code related problem.

https://stackoverflow.com/questions/71374284/angular-api-request-to-net-5-api-neterr-connection-refuse

conf.d

server {
    if ($host = www.modernamedia.no) {
        return 301 https://modernamedia.no$request_uri;
    } # managed by Certbot

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

    # Redirect to the correct place, if needed
    set $https_redirect 0;
    if ($server_port = 80) { set $https_redirect 1; }
    if ($host ~ '^www\.') { set $https_redirect 1; }
    if ($https_redirect = 1) {
        return 301 https://modernamedia.no$request_uri;
    }

    listen 80;
    server_name modernamedia.no;
    return 404; # managed by Certbot
}


server {
    listen [::]:443 ssl http2 ipv6only=on;
    listen 443 ssl http2; # managed by Certbot
    server_name modernamedia.no;
    location / {
        proxy_pass http://localhost:4000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
    ssl_certificate /etc/letsencrypt/live/modernamedia.no/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/modernamedia.no/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    # ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}


server {
    listen   80;
    server_name  www.modernamedia.no;

    return 301 https://modernamedia.no$request_uri;
}

server {
    listen        81;
    server_name   api.modernamedia.no;
    root /var/www/ModernaMedia/DotNet;
    location / {
        proxy_pass         http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}


sites-available/default

server {
    listen        81;
    server_name   api.modernamedia.no;
    root /var/www/ModernaMedia/DotNet;
    location / {
        proxy_pass         http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}

modernamedia.service

[Unit]
Description=ModernaMedia Net5 service
[Service]
WorkingDirectory=/var/www/ModernaMedia/DotNet
ExecStart=/usr/bin/dotnet /var/www/ModernaMedia/DotNet/ModernaMediaDotNet.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=ModernaMedia-dotnet
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target

And i can reach my .NET server through curl

Score:1
us flag

You should make sure Certbot does not touch your nginx configuration files. It uses a problematic and fragile approach to configure things.

Use the following approach instead:

# Redirect HTTP requests to HTTPS
server {
    listen 80;
    server_name modernamedia.no www.modernamedia.no;

    # Allow serving of Letsencrypt HTTP auth challenges

    location /.well-known {
        try_files $uri $uri/ =404;
    }

    # Do redirect to https
    location / {
        return 301 https://modernamedia.no$request_uri;
    }
}

# Redirect https://www.modernamedia.no to https://modernamedia.no
server {
    listen 443 ssl http2;
    server_name www.modernamedia.no;

    ssl_certificate /path/to/ssl_cert;
    ssl_certificate_key /path/to/ssl_key;

    return 301 https://modernamedia.no$request_uri;
}

# https://modernamedia.no
server {
    listen 443 ssl http2;
    server_name modernamedia.no;

    ssl_certificate /path/to/ssl_cert;
    ssl_certificate_key /path/to/ssl_key;

    # Actual web site configuration here
}
Stanley avatar
id flag
Thank you! I dont know what the issue was, but your config definitely worked!
Stanley avatar
id flag
The api.modernamedia.no still does not work for redirecting the .net backend
us flag
Please open a new question on the dot.net application, with clear explanation what "does not work" exactly means.
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.