美文网首页
nginx 反向代理处理跨域

nginx 反向代理处理跨域

作者: JUN888 | 来源:发表于2022-01-20 00:20 被阅读0次
server {
        listen        8585;
        server_name  localhost;
        #root   "E:/develop/www/testphp";
        location / {
            #index index.php index.html;  
             
            #include E:/develop/www/testphp/nginx.htaccess;
            #autoindex  off;
        }
        
        location ^~/web/{
            proxy_pass http://localhost:8080/;
        }
        
        location ^~ /apis/{
            proxy_pass http://localhost:5500/;
        }       
        
         
}

在浏览器里输入 http://localhost:8085/web/ 默认就会打开 http://localhost:8080

页面里的接口调用如

http://localhost:5500 需要写成 http://localhost:8085/apis/list

**Nginx会反向代理 调用 http://localhost:5500 **

vue-cli 的服务和express的服务都需要代理,如果只代理接口不代理页面是不行的

https://blog.csdn.net/zxd1435513775/article/details/102508463

nginx.conf 配置 前后代码 反向代理

# 访问地址
server {
        listen        81;
        server_name  localhost;
        #root   "F:/longyijun/www/test";
        location / {
            #index index.php index.html error/index.html;
             
            #include F:/longyijun/www/test/nginx.htaccess;
            #autoindex  off;
        }
        
        # 前端代理
        location ^~/web/{
            proxy_pass http://localhost:82/;
        }
        
        # 后端代理
        location ^~/apis/{
            proxy_pass http://localhost:91/;
        }  
}
# 后端代码
server {
        listen        91;
        server_name  localhost;
        root   "F:/longyijun/www/gl_leng_service/public";
        location / {
            index index.php index.html error/index.html;
            
            #include F:/longyijun/www/test/nginx.htaccess;
            include F:/longyijun/www/gl_leng_service/public/nginx.htaccess;
            autoindex  off;
        }
        
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9002;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}
# 前端代码
server {
        listen        82;
        server_name  test.com;
        root   "F:/longyijun/www/test";
        
        location / {
            index index.html error/index.html;
             
        } 
         
}

或者前端直接代理后台

# 前端代码
server {
        listen        82;
        server_name  test.com;
        root   "F:/longyijun/www/test";
        
        location / {
            index index.html error/index.html;
            
            #include F:/longyijun/www/test/nginx.htaccess;
            #autoindex  off;
        }
        
        location ^~/apis/{
            proxy_pass http://localhost:91/;
        } 
         
}

相关文章

网友评论

      本文标题:nginx 反向代理处理跨域

      本文链接:https://www.haomeiwen.com/subject/afndhrtx.html