您好,基本配置可能如下所示:
server {
listen 8080;
return 200 "Blog \n";
}
server {
listen 8081;
return 200 "About and Contact \n";
}
server {
listen 80;
location = /about {
proxy_pass http://localhost:8081;
}
location /blog {
proxy_pass http://localhost:8080;
}
}
让我给它添加一些文字:)
如果您只想拥有一个位置 /about,而该位置中没有任何其他内容,您可以使用 = /about
尝试在/about 后面添加一些内容,例如/about/foo,将导致 404。
如果您想将博客代理到其他位置,请使用/blog。使用此配置,您可以随意向您的位置添加一些内容,例如 /blog/test/,它仍会以 200 响应。
[root@localhost conf.d]# curl -v localhost/blog/test
* About to connect() to localhost port 80 (#0)
* Trying ::1...
* Connection refused
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /blog/test HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.17.6
< Date: Tue, 17 Mar 2020 06:09:31 GMT
< Content-Type: application/octet-stream
< Content-Length: 12
< Connection: keep-alive
<
/blog/test
* Connection #0 to host localhost left intact
开发中的无服务器方式
在你的 react 应用中使用某些东西将流量路由到正确的目标。
https://create-react-app.dev/docs/proxying-api-requests-in-development#configuring-the-proxy-manually
我为我的一个项目做了这个,它的工作很棒!
我将分享我的文件,但不会根据您的问题进行调整,仅作为一般参考和想法。
const proxy = require('http-proxy-middleware');
module.exports = function (app) {
app.use('/api', proxy({
target: 'http://localhost:8080',
changeOrigin: true,
}));
app.use('/img', proxy({
target: 'http://localhost:8080',
changeOrigin: true,
}));
app.use('/oauth', proxy({
target: 'http://localhost:8080',
changeOrigin: true,
}));
};
此文件 (setupProxy.js) 位于根级别的 thr src 目录中。
用于生产
我只知道一些与 node.js 之上的 express 的集成。由于代理与 http-protocoll 密切相关,因此您需要一些了解 http 协议的服务器组件。
在过去的几个月里,我注意到使用快速网络服务器作为代理的一些缺点。缓存在 NGINX 级别上更有效。如果路由复杂度增加,NGINX 可以更好地处理它。