Score:0

Host app on sub directory of main app

gb flag

So I have my main site that's built with react and express. Now I want to add a blog part to my site that's built with next.js. However I don't want to host it on something like blog.example.com I would rather host it on example.com/blog.

I'm using a digital ocean droplet that's running ubuntu 18.04 and nginx. I've looked at similar questions as mine but I couldn't get this working. I've pasted my nginx config below and added a code mark to the location block not working.

server {

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;
    server_name example.com; # managed by Certbot

        location / {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-NginX-Proxy true;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection upgrade;
                proxy_max_temp_file_size 0;
                proxy_pass http://my_nodejs_upstream/;
                proxy_redirect off;
                proxy_read_timeout 240s;
        }

        location /api {
                proxy_pass http://localhost:3001;
        }
        
        # THIS BLOCK IS NOT WORKING 
        location /blog {
                root /blog; # /blog dir is in the root folder           
        }


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
 }

I'm pretty new to nginx configuration so apologies if this is a simple mistake. Thank you.

Richard Smith avatar
jp flag
The path to the file is the concatenation of `root` value with the URI. Your configuration places `/blog/foo` at `/blog/blog/foo`. You should probably use `root /;` to point to the `blog` directory in the root of your file system.
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.