【问题标题】:Docker-compose, getting go api and nginx to communicate. Port troubledocker-compose,让 go api 和 nginx 通信。端口问题
【发布时间】:2020-06-09 16:50:11
【问题描述】:

我有一个 nginx 网络服务器正在运行,并且有一个 golang api 作为后端。

目前我的 web 应用程序在 braurl.se 上运行。

您可以在http://braurl.se:8080/获取数据

您可以在https://braurl.se查看前端

我在从后端获取数据时遇到问题,而且我的端口配置似乎搞砸了

我不想公开 8080 端口,而是希望能够使用 braurl.se/api/ 获取数据

我认为我做错的是下面显示的任何文件中的端口和代理密码

这是我的文件,谁能指出我在哪里以及我做错了什么:

Nginx 配置文件:

server {
    listen      80;
    listen [::]:80;
    server_name braurl.se www.braurl.se;

    location / {
        # This redirs to either www.braurl.se or braurl.se but with https.
        rewrite ^ https://$host$request_uri? permanent;
    }

    #for certbot challenges (renewal process)
    location ~ /.well-known/acme-challenge {
        allow all;
        root /data/letsencrypt;
    }


    location /api/ {

        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_pass http://goservice:8080;
        fastcgi_buffers 16 16k; 
        fastcgi_buffer_size 32k;

    }

}



#https://braurl.se
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name braurl.se;

    server_tokens off;

    ssl_certificate /etc/letsencrypt/live/braurl.se/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/braurl.se/privkey.pem;

    ssl_buffer_size 8k;

    ssl_dhparam /etc/ssl/certs/dhparam-2048.pem;

    ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
    ssl_prefer_server_ciphers on;

    ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;

    ssl_ecdh_curve secp384r1;
    ssl_session_tickets off;

    # OCSP stapling
    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 8.8.8.8;

    root /usr/share/nginx/html;
    index index.html;

    # Always try index files, this is for React.
    location / {
        try_files $uri /index.html;
    }

    location /api/ {

        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_pass http://goservice:8080;
        fastcgi_buffers 16 16k; 
        fastcgi_buffer_size 32k;

    }


}

Docker 编写文件

version: '3.1'

services:
  goservice:
    build: "."
    image: golang
    container_name: goservice
    expose:
      - "80"
    ports:
      - "8080:8080"   
  production-nginx-container:
    container_name: 'production-nginx-container'    
    image: nginx:latest
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./production.conf:/etc/nginx/conf.d/default.conf
      - ./production-site:/usr/share/nginx/html
      - ./dh-param/dhparam-2048.pem:/etc/ssl/certs/dhparam-2048.pem
      - /docker-volumes/etc/letsencrypt/live/braurl.se/fullchain.pem:/etc/letsencrypt/live/braurl.se/fullchain.pem
      - /docker-volumes/etc/letsencrypt/live/braurl.se/privkey.pem:/etc/letsencrypt/live/braurl.se/privkey.pem
    depends_on:
      - "goservice"

Dockerfile (golang):

FROM golang:1.12.7-alpine3.10 AS build
# Support CGO and SSL
RUN apk --no-cache add gcc g++ make
RUN apk add git
WORKDIR /go/src/app
COPY . .
RUN go get github.com/gorilla/mux
RUN GOOS=linux go build -ldflags="-s -w" -o ./bin/test ./main.go

FROM alpine:3.10
RUN apk --no-cache add ca-certificates
WORKDIR /usr/bin
COPY --from=build /go/src/app/bin /go/bin
EXPOSE 8080
ENTRYPOINT /go/bin/test --port 8080

【问题讨论】:

    标签: docker go nginx docker-compose


    【解决方案1】:

    NGinx 在路径上应用优先级,意味着如果从顶部开始的路径匹配,它不会检查后续路径。 location / 应该一直放在最后。

    容器应该共享一个网络以便能够互相看到,而不必暴露或与主机共享端口。

    NGinx 配置:

    server {
        listen      80;
        listen [::]:80;
        server_name braurl.se www.braurl.se;
    
        #for certbot challenges (renewal process)
        location ~ /.well-known/acme-challenge {
            allow all;
            root /data/letsencrypt;
        }
    
        location / {      # Always at this end (everything else)
            # This redirs to either www.braurl.se or braurl.se but with https.
            rewrite ^ https://$host$request_uri? permanent;
        }
    }
    
    -----------------------
    
    #https://braurl.se
    server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name braurl.se www.braurl.se;
    
        server_tokens off;
    
        ssl_certificate /etc/letsencrypt/live/braurl.se/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/braurl.se/privkey.pem;
    
        ssl_buffer_size 8k;
    
        ssl_dhparam /etc/ssl/certs/dhparam-2048.pem;
    
        ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
        ssl_prefer_server_ciphers on;
    
        ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;
    
        ssl_ecdh_curve secp384r1;
        ssl_session_tickets off;
    
        # OCSP stapling
        ssl_stapling on;
        ssl_stapling_verify on;
        resolver 8.8.8.8;
    
        root /usr/share/nginx/html;
        index index.html;
    
        location /api/ {    # this First, NGinx use priority, if path match, it won't check the next path
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_pass http://goservice:8080;
            fastcgi_buffers 16 16k;
            fastcgi_buffer_size 32k;
        }
    
        # Always try index files, this is for React.
        location / {      # Always at this end (everything else)
            try_files $uri /index.html;
        }
    }
    

    docker-compose.yml

    version: '3.1'
    
    services:
      goservice:
        build: "."
        image: golang
        container_name: goservice
        expose:
          - "8080" # <-- change port number, Dockerfile EXPOSE 8080
        networks:       # <-- Add this
          - random_name # <-- Add this
        # ports:          # <-- To Remove
        #   - "8080:8080" # <-- To Remove
    
      production-nginx-container:
        container_name: 'production-nginx-container'
        image: nginx:latest
        ports:
          - "80:80"
          - "443:443"
        volumes:
          - ./production.conf:/etc/nginx/conf.d/default.conf
          - ./production-site:/usr/share/nginx/html
          - ./dh-param/dhparam-2048.pem:/etc/ssl/certs/dhparam-2048.pem
          - /docker-volumes/etc/letsencrypt/live/braurl.se/fullchain.pem:/etc/letsencrypt/live/braurl.se/fullchain.pem
          - /docker-volumes/etc/letsencrypt/live/braurl.se/privkey.pem:/etc/letsencrypt/live/braurl.se/privkey.pem
        depends_on:
          - "goservice"
        networks:       # <-- Add this
          - random_name # <-- Add this
    
    networks:
      - random_name:
    

    现在您可以使用https://braurl.se 访问前端,使用https://braurl.se/api/ 访问API

    【讨论】:

    • 谢谢琼。你解释得很好,我终于让它运行起来了。我是一名网络开发人员,服务器对我来说是新的。你让我明白了,解决方案奏效了。再次感谢!
    猜你喜欢
    • 2023-03-17
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 2021-05-09
    • 2021-12-28
    • 2019-05-19
    相关资源
    最近更新 更多