登录
首页 >  Golang >  Go问答

使用 Docker-compose 在 go api 和 nginx 之间进行通信时出现端口问题

来源:stackoverflow

时间:2024-03-09 14:09:26 280浏览 收藏

从现在开始,努力学习吧!本文《使用 Docker-compose 在 go api 和 nginx 之间进行通信时出现端口问题》主要讲解了等等相关知识点,我会在golang学习网中持续更新相关的系列文章,欢迎大家关注并积极留言建议。下面就先一起来看一下本篇正文内容吧,希望能帮到你!

问题内容

我有一个正在运行的 nginx web 服务器和一个 golang api 作为后端。

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

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

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

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

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

我相信我做错的是下面显示的任何文件中的端口和 proxypass

这是我的文件,任何人都可以指出我在哪里以及我做错了什么:

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

解决方案


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

今天关于《使用 Docker-compose 在 go api 和 nginx 之间进行通信时出现端口问题》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>