简体   繁体   中英

Nginx Proxy Pass on SSL is not Considering Listening URL

I am currently trying to set up nginx virtual server blocks, which I have me absolutely ripping my hair out.

Essentially, I have a NodeJS instance running on a droplet at DigitalOcean. The point of this application, is to host both a website as well as an api. I want both of these to run on the same droplet.

I have set up my NodeJS app with express to create the routing and so on, and then create an http server with the express app. As my explanation might cause confusion, I have included the essentials of the code below:

const API_PORT = 8080;
const WEB_PORT = 8081;

const api = express();
const web = express();

web.use(express.static(path.join(__dirname, '../build')));

web.get('//', (req, res) => {
    res.sendFile(path.join(__dirname, '../build', 'index.html'));
})

api.use((req, res, next) => {
    const ip = (req. headers['x-forwarded-for'] || '').split(',').pop()
    || req.connection.remoteAddress
    || req.socket.remoteAddress
    || req.connection.socket.remoteAddress

    if (req.headers.auth !== AUTHCODE) {
        httpError(400, 'Validation failed');
        console.warn('Bad Auth Code');
        console.warn(req.headers.auth);
        console.warn(ip);
        return (res.json('Validation failed'));
    }

    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', '*');
    res.setHeader('Access-Control-Allow-Headers', '*');
    next();
});

Graph.route(api);

const apiServer = http.createServer(api);
const webServer = http.createServer(web);

apiServer.listen(WEB_PORT, '127.0.0.1', () => {
    console.log(`API Server is running on port ${API_PORT}`);
});
webServer.listen(API_PORT, '127.0.0.1', () => {
    console.log(`WEB Server is running on port ${WEB_PORT}`);
});

What I want to achieve is shown in the code above, but essentially I am trying to have a my api server listening to port 8080, and my website server to listen on port 8081.

I was told that I could use nginx to create a proxy_pass for this although I have not managed to make it work as intended. Below you see two examples of what I tried to do ! Anyone able to help me out would forever be my hero !

server {
       listen lace.guide:443 ssl;
       server_name lace.guide www.lace.guide;
       ssl_certificate /var/my-server/ssl/myapp.crt;
       ssl_certificate_key /var/my-server/ssl/myapp.key;
       ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
       location / {
                proxy_pass http://127.0.0.1:8080/$request_uri;
                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 api.lace.guide:443 ssl default_server;
       server_name api.lace.guide www.api.lace.guide;
       ssl_certificate /var/my-server/ssl/myapp.crt;
       ssl_certificate_key /var/my-server/ssl/myapp.key;
       ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
       location / {
                proxy_pass http://127.0.0.1:8081/$request_uri;
                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;
        }
}

I am also having another quite peculiar error which means that if I access my api.lace.url/something/somethingElse it actually hits //something/somethingElse on my server (console.log from req.url )

在此处输入图片说明

Here is an example of how server and client live on one server and are being managed with nginx (ssl setup omitted for simplicity). All request prefixed with /api get routed to the server and the rest - to the client.

upstream client {
    server client:3000;
}

upstream api {
    server api:5000;
}

server {
    listen 80;

    location / {
        proxy_pass http://client;
    }

    location /api {
        proxy_set_header X-Forwarded-For $remote_addr;
        rewrite /api/(.*) /$1 break;
        proxy_pass http://api;
    }    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM