skip to Main Content

I have the following nginx config:

server {
    listen 80;
    listen 443 ssl;
    
    ssl on;
        ssl_certificate /home/tom/local.tjrhodes.com.pem; 
    ssl_certificate_key /home/tom/local.tjrhodes.com-key.pem;

    root /home/tom/Nextcloud/local.tjrhodes.com/webroot/;
    
    error_log /home/tom/Nextcloud/local.tjrhodes.com/logs/error.log;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name local.tjrhodes.com;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
    
    location /pat/ {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $host;
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:3000;
            proxy_redirect off;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
    }

}

I call the /pat/ path with fetch on the client but the port 3000 on the proxy_pass command is overwritten with 443 as I’m testing locally over https. What am I missing in the config?
`

I’ve tried the node server calling it directly with the correct port (127.0.0.1:3000) and it responds ok. I can’t make the request from the client when connecting over https though.

2

Answers


  1. Chosen as BEST ANSWER

    Ok, for completeness here is the config that works for me...

    server {
        listen 443 ssl;
        listen [::]:443 ssl;
        
        ssl_certificate /data/home/tom/local.tjrhodes.com+3.pem; 
        ssl_certificate_key /data/home/tom/local.tjrhodes.com+3-key.pem;
        
        server_name local.tjrhodes.com;
        
        root "/data/home/tom/Cloud/local.tjrhodes.com/webroot/";
        
        index index.html;
        
        location / {
            try_files $uri $uri/ =404;
        }
        
        location ^~ /pat/   {
            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;
        }
    
    }
    
    server {
        listen 80;
        listen [::]:80;
        
        server_name local.tjrhodes.com;
        
        return 301 https://local.tjrhodes.com$request_uri;
    }
    

  2. change your nginx config to this:

    server {
        listen 80;
        listen 443 ssl;
    
        ssl_certificate /home/tom/local.tjrhodes.com.pem; 
        ssl_certificate_key /home/tom/local.tjrhodes.com-key.pem;
    
        root /home/tom/Nextcloud/local.tjrhodes.com/webroot/;
    
        error_log /home/tom/Nextcloud/local.tjrhodes.com/logs/error.log;
    
        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;
    
        server_name local.tjrhodes.com;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        location /pat/ {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_pass http://127.0.0.1:3000;
            proxy_redirect off;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }
    
    

    removed depricated on ssland added proxy_set_header X-Forwarded-Proto $scheme; to forward the original protocol (HTTP or HTTPS) of the client request to the backend server

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search