0

Ever since I set up HTTPS through SSL in my sites-enabled/default config, Nginx is reading from /usr/share/www/html rather than /var/www/html. There are no references to /usr/.../html in any of my files (when I use grep -r) so I have no clue why this is happening.

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name MY_WEBSITE.TLD; # Before you ask, yes
    return 302 https://$server_name$request_uri;
    root /var/www/html;
}

server {
    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;
    include snippets/self-signed.conf;
    include snippets/ssl-params.conf;

    index index.html index.htm index.nginx-debian.html index.php;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

Clearly I've defined the root. Why isn't it working?

Don't Panic
  • 13,965
  • 5
  • 32
  • 51
ColonelHedgehog
  • 438
  • 3
  • 18

2 Answers2

1

Your config describes 2 separate servers - not 2 ports on one server. It is obviously convenient that you can include them in a single file in sites-available/, but technically they are different, independent servers, and you could split each server block into a different sites-available/ config file.

Bearing that in mind, check what you have for each server. You have one server on port 80, which has a root specified. All fine there.

You have another server on port 443. This is a completely separate server, not the same server on another port. It has no root specified, so nginx will fall back to some default root location, determined by compile-time switches or distro-specific variables, as described in this SO question.

Don't Panic
  • 13,965
  • 5
  • 32
  • 51
  • But when I specify a root, it still uses usr/.../html. EDIT: I specified as the fifth line in the second (SSL) server{} block. – ColonelHedgehog Dec 22 '17 at 01:02
  • 1
    @ColonelHedgehog The key is *where* did you specify a `root`? And did you restart nginx after doing that? – Don't Panic Dec 22 '17 at 01:03
  • OK, after a refresh, it seems to be using the correct root. However... now it downloads my php files instead of viewing them. So it looks like I've traded one problem for another. – ColonelHedgehog Dec 22 '17 at 01:06
  • @ColonelHedgehog @ColonelHedgehog Yes, that's a new problem. My guess is your `location /` is overriding the `location ~ \.php$`. Do you really need both? Try moving your `try_files $uri $uri/ =404;` into your `location ~ \.php$`, and deleting the `location /`. – Don't Panic Dec 22 '17 at 01:17
  • Didn't seem to change anything. Here is my current file: https://pastebin.com/9giy4yZ2 - Note: if I add another "server_name" declaration in the second server{} block, then it gives me 502 bad gateway but does NOT download the files. – ColonelHedgehog Dec 22 '17 at 01:28
  • 1
    @ColonelHedgehog 1. Don't specify `root` 2x inside the same `server`. 2. Having 2x `default_server` inside one `server` block also looks strange, try removing them both for now. 3. What is `/scripts` in your `fastcgi_param`? Typically that is something like `$document_root$fastcgi_script_name;`. 4. I think you need a `server_name` - what are you using, and check the logs, what is the real error? – Don't Panic Dec 22 '17 at 01:41
  • Another change I made. It turns out there was another server {} block in my nginx.conf. I made changes so both files look like this: https://pastebin.com/GHTLL8Vj EDIT: I didn't see your response yet. Trying those now. – ColonelHedgehog Dec 22 '17 at 01:42
  • Turned out I had the wrong directory for php-fpm specified. Wow. – ColonelHedgehog Dec 22 '17 at 01:49
0

Aaaaand it all leads back to me specifying the wrong directory for php7.0-fpm. Because of course it does! Thank you to @Don't Panic and @Keven_Kinsey for helping me out. It finally loads properly.

ColonelHedgehog
  • 438
  • 3
  • 18