0

My config file is located in /etc/nginx/sites-enabled is called songreccs.conf and looks like this:


server {
        server_name www.songreccs.com songreccs.com;


        location /static {
                root /home/user/sonreccs/flaskrec/;
        }

        location / {
                proxy_pass http://localhost:8000;
                include /etc/nginx/proxy_params;
                proxy_redirect off;
        }


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/songreccs.com/fullchain.pem; # manage>
    ssl_certificate_key /etc/letsencrypt/live/songreccs.com/privkey.pem; # mana>
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}

server {
        listen 80;
        server_name www.songreccs.com songreccs.com;
    if ($host = www.songreccs.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    return 404; # managed by Certbot


}

I thinks that's what the file is doing:

The first block listens on port 443 and should redirect everything to what's specified in the location / block. The location /static should give instructions where to find the static files.

The second block listens to port 80 and redirects everything coming in, to port 443. Which is then again taken care of by the first block.

The path of the static files are located at home/user/sonreccs/flaskrec/static.

The error I'm getting on the website is:

[Error] Failed to load resource: the server responded with a status of 403 (Forbidden) (styles.css, line 0)

which didn't occur before using certbot to install the certificate. How can I change it, so it is allowed again?

maltek
  • 3
  • 2
  • Please be more specific about `isn't served anymore`. What is the exact error message you encounter? – Gerald Schneider Aug 01 '22 at 06:58
  • im not getting an error message. I can access the website via my browser. It just doesn't display any of the css anymore. In the background there is a flask app running with Gunicorn + supervisor; this still is functional. So I think Nginx has to be the culprit. I also tried with "alias" instead of "root" and changing the path according to [this article](https://stackoverflow.com/questions/10631933/nginx-static-file-serving-confusion-with-root-alias), but that did not help @GeraldSchneider – maltek Aug 01 '22 at 07:39
  • Check the developer console of your browser for error messages for these requests. – Gerald Schneider Aug 01 '22 at 07:41
  • okay, thank you so far! It shows "Failed to load resource: the server responded with a status of 403 (Forbidden)". Since it worked with http, does this mean the path ist forbidden only for https? – maltek Aug 01 '22 at 07:53
  • Your server blocks for port 80 and 443 have mismatching server names. It's possible another server block is active which receives the requests. – Gerald Schneider Aug 01 '22 at 08:12
  • Your first `server` block has no `listen` directive. The configuration looks inconsistent in other ways too. – Tero Kilkanen Aug 01 '22 at 19:48
  • I updated the question and entered the original config file (before running certbot --nginx ..). And regarding the inconsistencies: yes. After trying anything I read, I just left it in this state. I updated that too. – maltek Aug 02 '22 at 18:14

1 Answers1

0

Try this below Nginx Configuration:

server {
        listen 443 ssl;
        server_name www.songreccs.com songreccs.com;

        location /static/ {
                root /home/user/sonreccs/flaskrec;
        }

        location / {
                include /etc/nginx/proxy_params;
                proxy_pass http://localhost:8000;
                proxy_redirect off;
        }

        ssl_certificate /etc/letsencrypt/live/songreccs.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/songreccs.com/privkey.pem;
        include /etc/letsencrypt/options-ssl-nginx.conf;
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}

server {
        listen 80;
        server_name www.songreccs.com songreccs.com;
        if ($host = www.songreccs.com) {
            return 301 https://$host$request_uri;
        }
        return 404;
}

Permission for the static files:

chmod 755 /home/user && chmod 755 /home/user/sonreccs && chmod 755 /home/user/sonreccs/flaskrec && chmod 755 /home/user/sonreccs/flaskrec/static/
soup
  • 76
  • 4
  • Hey! thanks for the answer. I tried removing the `listen 80;` int the 3rd block. It doesn't change anything. But I´ll update the question again to show my current config file. I removed the first block completely, and it still works like it has before. Still not showing the css. – maltek Aug 03 '22 at 09:39
  • @maltek Try this Nginx Configuration. – soup Aug 03 '22 at 10:47
  • ist that exactly what i wrote in my updated question? – maltek Aug 03 '22 at 10:49
  • @maltek no not exact, just try it, copy and paste to the config – soup Aug 03 '22 at 10:51
  • I did. Does exactly the same. Its not worse though, looks a bit nicer though.. I think the problem is with setting the permissions for Nginx to allow access to the files. I just don't know why the permissions need to be updated when it did work before. – maltek Aug 03 '22 at 10:56
  • @maltek Try now with the given command. – soup Aug 03 '22 at 11:03
  • Thank you!! I tried stuff with chmod before, but it didn't work! Wow, that's such a relief, thanks so much! I will look into that chmod stuff again for sure! – maltek Aug 03 '22 at 11:18
  • @maltek Your welcome, upvote the answer. – soup Aug 03 '22 at 11:25
  • I can't, because I don't have 15 reputation yet. But I accepted it – maltek Aug 03 '22 at 11:26