WDHK 2025-05-11 22:45 采纳率: 87.5%
浏览 4

nginx+vue+spingboot端口配置问题,

nginx+vue+spingboot配置问题,我的项目前端一个论坛套着一个AI模块(等于一个前端),后端论坛与AI使用不同的后端都叫/api(并且不能更改)但是端口有6060,7070两个,在nginx中只能启动一个端口应该怎么解决


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
  
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }

         location ~ /api/{
    proxy_pass http://localhost:7070; # 后端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;
    proxy_set_header X-Forwarded-Proto $scheme;
  }   
  location ~ /api/ {
   proxy_pass http://localhost:6060;
   proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
  }
 
  #location / {  # 修改入口路径
   # proxy_pass http://localhost:6060/;  # 显式指定后端路径
#proxy_set_header Host $host;
   # proxy_set_header X-Real-IP $remote_addr;
    #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #proxy_set_header X-Forwarded-Proto $scheme;
}


        #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 ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # 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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}





  • 写回答

6条回答 默认 最新

  • 阿里嘎多学长 2025-05-11 22:45
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    问题解答

    你遇到的问题是,使用 Nginx 配置多个端口,分别用于 Vue 前端和 SpringBoot 后端的 API 服务。

    根据你的描述,前端是一个论坛套着一个 AI 模块,后端有两个 API 服务,分别使用不同的端口 6060 和 7070。

    解决方案:

    1. 在 Nginx 配置文件中,创建两个 server 块,每个 server 块负责一个端口。
    http {
        ...
        server {
            listen 6060;
            location / {
                proxy_pass http://localhost:8080;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
            }
        }
    
        server {
            listen 7070;
            location / {
                proxy_pass http://localhost:8081;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
            }
        }
    }
    

    其中,listen 指令指定了 Nginx 监听的端口,location 指令指定了请求的 URL,proxy_pass 指令将请求转发到对应的后端服务器。

    1. 在 Vue 前端中,使用 axiosfetch 库发送请求时,需要指定正确的端口号。
    axios.create({
      baseURL: 'http://localhost:6060' // 或 'http://localhost:7070'
    })
    .get('/api/data')
      .then(response => {
        console.log(response.data);
      })
      .catch(error => {
        console.error(error);
      });
    
    1. 在 SpringBoot 后端中,使用 @SpringBootApplication 注解启用 Spring Boot 应用程序,并在 application.properties 文件中配置端口号。
    server.port=8080 // 或 8081
    

    这样,Nginx 就可以将请求转发到对应的后端服务器,Vue 前端可以正确地发送请求到后端服务器。

    评论

报告相同问题?

问题事件

  • 创建了问题 5月11日