在同一容器中运行nginx和node js

sg3maiej  于 2023-01-16  发布在  Nginx
关注(0)|答案(1)|浏览(209)

你好,我正在尝试运行节点js和nginx作为代理服务器。我可以让他们工作,如果他们都在不同的容器同时运行。但当试图 Package 和运行他们都在同一个容器不知何故失败。
这是我用来构建它的dockerfile。

FROM nginx
#install node 14.19.1
RUN apt update
RUN apt install wget -y
RUN apt install xz-utils -y
RUN wget https://nodejs.org/download/release/v14.19.1/node-v14.19.1-linux-x64.tar.xz
RUN tar -xvf node-v14.19.1-linux-x64.tar.xz
RUN cp -r node-v14.19.1-linux-x64/bin /usr/
RUN cp -r node-v14.19.1-linux-x64/include /usr/
RUN cp -r node-v14.19.1-linux-x64/lib /usr/
RUN cp -r node-v14.19.1-linux-x64/share /usr/
RUN export PATH=/usr/node-v14.19.1-linux-x64/bin:$PATH
#nginx conf  
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/default.conf
#run node js server
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "node", "server.js" ]

这一个使用节点js图像

FROM node:14.19.1
WORKDIR /usr/src/app
#install and configure nginx
RUN apt update
RUN apt install nginx -y
#COPY myserver.config /etc/nginx/sites-available
#RUN ln -s /etc/nginx/sites-available/myserver.config /etc/nginx/sites-enabled/
#RUN nginx -t 
#RUN service nginx restart
#RUN ufw allow 'Nginx Full'
#install and run node server
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 80
CMD [ "node", "server.js" ]

这是nginx.conf文件,我试图运行nginx通过我知道localhost:3000似乎不工作。你能建议我一个替代品,使其工作。谢谢

server{
    listen 80;
    server_name pnext;
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass 0.0.0.0:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        # location /overview {
        #     proxy_pass http://127.0.0.1:3000$request_uri;
        #     proxy_redirect off;
        # }
    }
}
eoigrqb6

eoigrqb61#

只需将CMD行更改为以下内容:

CMD nginx -g 'pid /tmp/nginx.pid;' & node server.js

相关问题