这是一个 Nginx 配置文件,用于将请求转发到不同的后端服务。它监听 8090 端口,并将所有请求转发到本地 8080 端口或者 7070 端口的不同路径下。

第一个 location 指令将根路径的请求转发到 http://localhost:8080/,并添加了跨域请求头。

第二个 location 指令将 /ueditor 路径下的请求转发到 http://127.0.0.1:7070/ueditor/jsp/controller/,同样添加了跨域请求头。

第三个 location 指令将 /apis/ 路径下的请求转发到 http://127.0.0.1:7070/,同样添加了跨域请求头。

示例代码:

server {
        listen       8090;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://localhost:8080/;
            add_header Access-Control-Allow-Origin *;

            add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';

            add_header Access-Control-Allow-Headers *;
        }

        location /ueditor {
            proxy_pass http://127.0.0.1:7070/ueditor/jsp/controller/;
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }

        location /apis/ {

	proxy_pass http://127.0.0.1:7070/;
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }
}
Nginx 配置文件:代理请求到不同后端服务

原文地址: https://www.cveoy.top/t/topic/nYDy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录