nginx相关

使用docker nginx代理前端,避免跨域问题

  • 使用dokcer-compose 启动nginx
    docker-compose.yml配置:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    nginx:
        container_name: nginx
        image: nginx:latest
        ports: 
          - 80:80
          - 443:443
        volumes: 
          - ./conf/nginx:/etc/nginx/conf.d
          - ./log/nginx:/var/log/nginx
          - ./static:/var/www
    

    nginx.conf配置:

    前端项目设置host header
    host.docker.internal是docker宿主机ip

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
    server {
     listen 80;
     server_name localhost; 
    
     # 代理前端项目
     location / {
         proxy_set_header Host $host;
         #docker nginx必须这样设置
         proxy_pass http://host.docker.internal:8080;
     }
    
     # 代理后端接口
     # 原先前端请求地址 http://localhost:9090/server/index
     # 修改成 http://localhost:9090/api/server/index
     location /api {
         # 接口地址修正
         rewrite ^/api/(.*)$ /$1 break;
         # 后端接口地址
         proxy_pass http://host.docker.internal:9090;
     }
    }
    
updatedupdated2021-07-122021-07-12
加载评论