Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

node.js - Nginx + React Router and proxy_pass

Does someone know how to set proxy_pass to exact path.

I have React app that works with node server.

const express = require('express'),
        app = express(),
        path = require('path'),
        bodyParser = require('body-parser'),
        router = require('./routes/route.index.js'),
        react = require('./routes/routes.app.js'),
        history = require('connect-history-api-fallback');
    
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(bodyParser.json());
    app.use(express.static(path.join(__dirname, './assets')));
    app.use(history());
    
    router(app, react);  // app.get(*, (req, res) => { res.sendFile(....) })
    
    app.listen(process.env.CLIENT_FRONTEND_PORT, () => console.info(`listen ${process.env.CLIENT_FRONTEND_PORT}`));

I have 2 domains 1 domain works great but for second I need to set proxy_pass to exact React router path.

Example:

example1.com/contact - works 

example2.com/ - should open /contact router immediately. 

I have tried for second domain:

location ^~ / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://frontend:8880/contact;
}

location ~* ^.+.(jpg|jpeg|gif|png|ico|css|pdf|ppt|txt|bmp|rtf|ttf|svg|js)$ {
        expires 2d;
        add_header Cache-Control public;
}

And others regexes with nginx but all the time received an error, because js files could not be found by url example2.com/build/.......js

Any clues?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...