如何在Docker、NextJS和Nginx中在隔离网络中创建正确的端点

huangapple go评论84阅读模式
英文:

How to create correct endpoints within an isolated network in Docker, NextJS and Nginx

问题

Nginx配置:

  1. worker_processes 1;
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. sendfile on;
  7. upstream api {
  8. server nextjs-app:3000;
  9. }
  10. server {
  11. listen 80 default_server;
  12. location / {
  13. proxy_pass http://api;
  14. proxy_set_header Host $host;
  15. proxy_set_header X-Real-IP $remote_addr;
  16. }
  17. }
  18. }

docker-compose:

  1. version: '3.8'
  2. services:
  3. nginx:
  4. image: nginx:latest
  5. restart: always
  6. depends_on:
  7. - rust-server
  8. - nextjs-app
  9. volumes:
  10. - ./nginx/nginx.conf:/etc/nginx/nginx.conf
  11. ports:
  12. - "80:80"
  13. networks:
  14. - app_network
  15. nextjs-app:
  16. build:
  17. context: ./client
  18. dockerfile: Dockerfile
  19. container_name: nextjs-app
  20. environment:
  21. - CHOKIDAR_USEPOLLING=true
  22. - DB_USER=${DB_USER}
  23. - DB_PASS=${DB_PASS}
  24. expose:
  25. - "3000"
  26. networks:
  27. - app_network
  28. rust-server:
  29. build:
  30. context: ./userserver
  31. dockerfile: Dockerfile
  32. container_name: rustserver-dc
  33. expose:
  34. - "10000"
  35. networks:
  36. - app_network
  37. volumes:
  38. - './userserver:/usr/src/userserver'
  39. - '/usr/src/userserver/target'
  40. environment:
  41. - MY_LOG_LEVEL=info
  42. - MY_LOG_STYLE=Always
  43. - DATABASE_URL=${DATABASE_URL}
  44. - STRIPE_SECRET_KEY=${STRIPE_SECRET_KEY}
  45. - STRIPE_PUBLISH_KEY=${STRIPE_PUBLISH_KEY}
  46. - ACCESS_TOKEN_SECRET=${ACCESS_TOKEN_SECRET}
  47. - REFRESH_TOKEN_SECRET=${REFRESH_TOKEN_SECRET}
  48. - RESET_PASSWORD_SECRET=${RESET_PASSWORD_SECRET}
  49. - REDIS_URL=${REDIS_URL}
  50. surrealdb:
  51. image: surrealdb/surrealdb
  52. environment:
  53. - DB_USER=${DB_USER}
  54. - DB_PASSWORD=${DB_PASSWORD}
  55. expose:
  56. - "8000"
  57. networks:
  58. - app_network
  59. volumes:
  60. - data:/var/lib/surrealdb/data
  61. command: "start --user ${DB_USER} --pass {DB_PASSWORD} --log full file://var/lib/surreal/data"
  62. networks:
  63. app_network:
  64. driver: bridge
  65. volumes:
  66. data:

Next.js首页页面:

  1. import Head from 'next/head';
  2. import axios from 'axios';
  3. import { useState, useEffect } from 'react';
  4. export default function Home() {
  5. const [data, setData] = useState(null);
  6. useEffect(() => {
  7. axios.get('http://rustserver-dc:10000')
  8. .then(res => {
  9. setData(res.data);
  10. })
  11. .catch(err => {
  12. console.log(err);
  13. })
  14. }, []);
  15. console.log(data);
  16. return (
  17. <>
  18. <Head>
  19. <title>Create Next App</title>
  20. <meta name="description" content="Generated by create next app" />
  21. <meta name="viewport" content="width=device-width, initial-scale=1" />
  22. <link rel="icon" href="/favicon.ico" />
  23. </Head>
  24. <main>
  25. <div>Welcome to the app</div>
  26. </main>
  27. </>
  28. )
  29. }

控制台中出现的错误:

  1. Failed to load resource: net::ERR_NAME_NOT_RESOLVED rustserver-dc:10000/:1

以上为您提供的翻译内容,不包括代码部分。

英文:

I have a Docker network that is an isolated network and reverse proxy that serves the NextJS app to the localhost. Unfortunately, fetch to the backend gets failed whenever I open the browser. However, curl http://rustserver-dc:10000/ responds with the data. I do not want the DB and backend to get exposed, only is available from NextJS app when browsing.

Nginx conf:

  1. worker_processes 1;
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. sendfile on;
  7. upstream api {
  8. server nextjs-app:3000;
  9. }
  10. server {
  11. listen 80 default_server;
  12. location / {
  13. proxy_pass http://api;
  14. proxy_set_header Host $host;
  15. proxy_set_header X-Real-IP $remote_addr;
  16. }
  17. }
  18. }

docker-compose:

  1. version: &#39;3.8&#39;
  2. services:
  3. nginx:
  4. image: nginx:latest
  5. restart: always
  6. depends_on:
  7. - rust-server
  8. - nextjs-app
  9. volumes:
  10. - ./nginx/nginx.conf:/etc/nginx/nginx.conf
  11. ports:
  12. - &quot;80:80&quot;
  13. networks:
  14. - app_network
  15. nextjs-app:
  16. build:
  17. context: ./client
  18. dockerfile: Dockerfile
  19. container_name: nextjs-app
  20. environment:
  21. - CHOKIDAR_USEPOLLING=true
  22. - DB_USER=${DB_USER}
  23. - DB_PASS=${DB_PASS}
  24. expose:
  25. - &quot;3000&quot;
  26. networks:
  27. - app_network
  28. rust-server:
  29. build:
  30. context: ./userserver
  31. dockerfile: Dockerfile
  32. container_name: rustserver-dc
  33. expose:
  34. - &quot;10000&quot;
  35. networks:
  36. - app_network
  37. volumes:
  38. - &#39;./userserver:/usr/src/userserver&#39;
  39. - &#39;/usr/src/userserver/target&#39;
  40. environment:
  41. - MY_LOG_LEVEL=info
  42. - MY_LOG_STYLE=Always
  43. - DATABASE_URL=${DATABASE_URL}
  44. - STRIPE_SECRET_KEY=${STRIPE_SECRET_KEY}
  45. - STRIPE_PUBLISH_KEY=${STRIPE_PUBLISH_KEY}
  46. - ACCESS_TOKEN_SECRET=${ACCESS_TOKEN_SECRET}
  47. - REFRESH_TOKEN_SECRET=${REFRESH_TOKEN_SECRET}
  48. - RESET_PASSWORD_SECRET=${RESET_PASSWORD_SECRET}
  49. - REDIS_URL=${REDIS_URL}
  50. surrealdb:
  51. image: surrealdb/surrealdb
  52. environment:
  53. - DB_USER=${DB_USER}
  54. - DB_PASSWORD=${DB_PASSWORD}
  55. expose:
  56. - &quot;8000&quot;
  57. networks:
  58. - app_network
  59. volumes:
  60. - data:/var/lib/surrealdb/data
  61. command: &quot;start --user ${DB_USER} --pass {DB_PASSWORD} --log full
  62. file://var/lib/surreal/data&quot;
  63. networks:
  64. app_network:
  65. driver: bridge
  66. volumes:
  67. data:

Next js index page:

  1. import Head from &#39;next/head&#39;
  2. import axios from &#39;axios&#39;;
  3. import { useState, useEffect } from &#39;react&#39;;
  4. export default function Home() {
  5. const [data, setData] = useState(null);
  6. useEffect(() =&gt; {
  7. axios.get(&#39;http://rustserver-dc:10000&#39;)
  8. .then(res =&gt; {
  9. setData(res.data);
  10. })
  11. .catch(err =&gt; {
  12. console.log(err);
  13. })
  14. }, []);
  15. console.log(data);
  16. return (
  17. &lt;&gt;
  18. &lt;Head&gt;
  19. &lt;title&gt;Create Next App&lt;/title&gt;
  20. &lt;meta name=&quot;description&quot; content=&quot;Generated by create next app&quot; /&gt;
  21. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot; /&gt;
  22. &lt;link rel=&quot;icon&quot; href=&quot;/favicon.ico&quot; /&gt;
  23. &lt;/Head&gt;
  24. &lt;main&gt;
  25. &lt;div&gt;Welcome to the app&lt;/div&gt;
  26. &lt;/main&gt;
  27. &lt;/&gt;
  28. )
  29. }

Error occurring in console:

Failed to load resource: net::ERR_NAME_NOT_RESOLVED rustserver-dc:10000/:1

Had the same issue when I was trying to connect two nodes in Kubernetes, and exposing only NextJS app via Nginx reverse proxy

答案1

得分: 1

如果您希望在 Docker 网络上获取数据并将其发送给客户端,您可以使用 getServerSideProps 来在页面请求时获取数据,或者使用 getStaticProps 在构建时检索数据。

  1. export async function getServerSideProps(context) {
  2. const res = await axios.get('http://rustserver-dc:10000')
  3. return {
  4. props: res.data, // 作为 props 传递给页面组件
  5. }
  6. }

如果您需要客户端在这些时间之外请求 rustserver-dc,您可以添加一个 API 路由 到 Next.js,以将请求代理到 rustserver-dc 服务。

英文:

If you want next to fetch the data on the docker network and send it to the client you could use getServerSideProps to fetch the data when the page is requested, or getStaticProps to retrieve the data build time.

  1. export async function getServerSideProps(context) {
  2. const res = await axios.get(&#39;http://rustserver-dc:10000&#39;)
  3. return {
  4. props: res.data, // will be passed to the page component as props
  5. }
  6. }

If you need the client to make requests for rustserver-dc outside of those times, you could add an API route to next that proxies the request onto the rustserver-dc service.

huangapple
  • 本文由 发表于 2023年2月19日 23:14:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定