Nick Briz
Nick Briz

Reputation: 1977

nginx won't change root to sub directory

I'm new to nginx and I'm trying to get my second.domain.com to display the contents of first.domain.com/dir, ( running on port 3000 of my localhost ) after looking online it seems this is the solution

# this one works fine
server {
    listen 80;

    server_name first.domain.com;

    location / {
         proxy_pass http://localhost:3000;
         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;
    }
 }

# this one doesn't work as expected
server {
    listen 80;

    server_name second.domain.com;

    location / {
         proxy_pass http://localhost:3000;
         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;
         root /dir;
         index index.html;
    }
 }

but when I visit second.domain.com I'm getting the same root as first.domain.com rather than first.domain.com/dir ...can anyone see what I'm doing wrong?

Upvotes: 1

Views: 2435

Answers (1)

Nick Briz
Nick Briz

Reputation: 1977

riffing off of mim's comment, I got it to work this way

# this one is now working as planned :)
server {
    listen 80;

    server_name second.domain.com;

    location / {

         # added mim's suggestiong here
         rewrite /folder/(.*) /$1 break; 

         # then added the folder after the port 
         proxy_pass http://localhost:3000/folder;

         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;
         root /dir;
         index index.html;
    }
 }

Upvotes: 2

Related Questions