为了账号安全,请及时绑定邮箱和手机立即绑定

docker-compose,让 go api 和 nginx 通信。端口问题

docker-compose,让 go api 和 nginx 通信。端口问题

Go
MMMHUHU 2022-05-18 14:40:59
我有一个正在运行的 nginx 网络服务器和一个 golang api 作为后端。目前,我在 braurl.se 运行了 Web 应用程序。您可以在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.seserver {    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;    }
查看完整描述

1 回答

?
慕的地6264312

TA贡献1817条经验 获得超6个赞

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;

    }

}

码头工人-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和使用 APIhttps://braurl.se/api/


查看完整回答
反对 回复 2022-05-18
  • 1 回答
  • 0 关注
  • 162 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号