账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    nginx如何配置才可以让同一域名不同端口的前后端进行交互。
    43
    0

    现在我的项目的前端是react,后端是ssm,前端在localhost:3000 ,后端在localhost:8080,
    也就是localhost:3000的请求如何转到localhost:8080,
    如何正确设置nginx的location
    以下是我的location配置:

       server { 
       listen 80; 
       
       server_name  localhost;
    
        #charset koi8-r;
    
        #access_log  logs/host.access.log  main;
    
        location / {
            root   html;
            index  index.html index.htm;  
        }
    
        #error_page  404              /404.html;
    
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        location ~ \.json$ {
            proxy_pass http://localhost:8080;
        }
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
    
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    
    请求大神详细解答,比较急!!
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 孤舟蓑笠翁 普通会员 1楼

      在Nginx中,可以使用location块来配置不同端口的前后端交互。以下是一个简单的示例:

      nginx location /api { proxy_pass http://localhost:3000/api; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }

      在这个示例中,location /api块定义了一个路由,指向http://localhost:3000/api端口。Nginx将这个请求转发到后端服务器,后端服务器可以是任何支持HTTP请求的服务器,例如使用Node.js和Express框架的服务器。

      你可以根据你的需求修改location块中的端口和后端服务器。例如,如果你需要将请求转发到后端服务器的其他端口,可以修改location块中的端口,如下所示:

      nginx location /api { proxy_pass http://localhost:8080/api; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }

      在这个示例中,location /api块将请求转发到http://localhost:8080/api端口。Nginx将这个请求转发到后端服务器的8080端口。

    更多回答
    网站公告
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部