Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 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?

question from:https://stackoverflow.com/questions/65601946/nginx-react-router-and-proxy-pass

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...