簡體   English   中英

如何創建我的nginx.conf

[英]How to create my nginx.conf

問題在於API和前端未鏈接。 啟動nginx容器時,我可以使用--link命令。 但是我仍然需要編輯我的nginx.conf。

此刻我有

worker_processes 4;

events { worker_connections 1024; }

http {

        upstream node-app {
              least_conn;
              server nodejs:8888 weight=10 max_fails=3 fail_timeout=30s;

        }

        server {
              listen 80;

              location / {
                proxy_pass http://node-app;
                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;
              }
        }
}

我的nodejs容器具有--name nodejs並在端口8888( -p 8888:8888 )上運行,它的dockerfile如下所示:

FROM node

# Create app directory
RUN mkdir -p /usr/src/www
WORKDIR /usr/src/www

# copy
COPY node_modules /usr/src/www/node_modules
COPY gulpfile.js /usr/src/www/gulpfile.js
COPY gulp.config.js /usr/src/www/gulp.config.js
COPY server.js /usr/src/www/server.js 

EXPOSE 8080
CMD [ "node", "server.js" ]

我首先執行:

docker run -d -p 8888:8888 --name nodejs localhost:5000/test/nodejs-image:1

我可以在localhost:8888 / api上訪問我的api

這是Nginx的Dockerfile(dist是在npm install等之后創建的,用於angular)

FROM nginx
COPY dist /usr/share/nginx/html/dist

當我剛表演時:

docker run -d -p 80:80 --name nginx localhost:5000/test/nginx-image:1

我可以在localhost:80 / dist /上訪問我的靜態文件。

但是我必須鏈接兩個容器+更改nginx.conf:

docker run -d -p 80:80 --name nginx --link nodejs:nodejs -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf localhost:5000/test/nginx-image:1

我的server.js包含以下內容:

app.post('/api/login', requestProxy({
  url: xxx + "/login"
}));

有人可以修復我的nginx.conf還是有其他錯誤?

一項更改:在您的nginx.conf中,將proxy_pass http:// node-app更改為proxy_pass http:// nodejs:8888

當您鏈接conatiner “ --link nodejs:nodejs”時 ,docker將使用“ conatiner_name container_ip”在/ etc / hosts文件中創建並輸入,因此您可以使用其名稱訪問(ping)鏈接的容器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM