orztv
commited on
Commit
·
026e3a8
1
Parent(s):
33ac2ae
update
Browse files- Dockerfile +16 -4
- app.py +5 -1
- nginx.conf +20 -0
- server.js +14 -0
Dockerfile
CHANGED
@@ -3,11 +3,10 @@ FROM nikolaik/python-nodejs:python3.10-nodejs20
|
|
3 |
WORKDIR /home/pn/app
|
4 |
|
5 |
# 复制 Python 和 Node.js 应用程序文件
|
6 |
-
COPY app.py .
|
7 |
-
COPY server.js .
|
8 |
|
9 |
# 安装依赖
|
10 |
-
RUN pip install flask
|
11 |
RUN npm install express
|
12 |
|
13 |
# 安装 Nginx
|
@@ -16,8 +15,21 @@ RUN apt-get update && apt-get install -y nginx
|
|
16 |
# 配置 Nginx
|
17 |
COPY nginx.conf /etc/nginx/nginx.conf
|
18 |
|
|
|
|
|
|
|
|
|
19 |
# 暴露端口
|
20 |
EXPOSE 7860
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
# 启动服务
|
23 |
-
CMD
|
|
|
|
|
|
3 |
WORKDIR /home/pn/app
|
4 |
|
5 |
# 复制 Python 和 Node.js 应用程序文件
|
6 |
+
COPY --chown=pn:pn app.py server.js ./
|
|
|
7 |
|
8 |
# 安装依赖
|
9 |
+
RUN pip install flask gunicorn
|
10 |
RUN npm install express
|
11 |
|
12 |
# 安装 Nginx
|
|
|
15 |
# 配置 Nginx
|
16 |
COPY nginx.conf /etc/nginx/nginx.conf
|
17 |
|
18 |
+
# 创建日志目录并设置权限
|
19 |
+
RUN mkdir -p /var/log/nginx /var/lib/nginx && \
|
20 |
+
chown -R pn:pn /var/log/nginx /var/lib/nginx
|
21 |
+
|
22 |
# 暴露端口
|
23 |
EXPOSE 7860
|
24 |
|
25 |
+
# 设置环境变量
|
26 |
+
ENV FLASK_PORT=6000
|
27 |
+
ENV NODE_PORT=7000
|
28 |
+
|
29 |
+
# 切换到非root用户
|
30 |
+
USER pn
|
31 |
+
|
32 |
# 启动服务
|
33 |
+
CMD nginx && \
|
34 |
+
gunicorn -b 0.0.0.0:$FLASK_PORT app:app & \
|
35 |
+
NODE_PORT=$NODE_PORT node server.js
|
app.py
CHANGED
@@ -6,5 +6,9 @@ app = Flask(__name__)
|
|
6 |
def hello():
|
7 |
return "Hello from Flask!"
|
8 |
|
|
|
|
|
|
|
|
|
9 |
if __name__ == '__main__':
|
10 |
-
app.run(host='0.0.0.0', port=6000)
|
|
|
6 |
def hello():
|
7 |
return "Hello from Flask!"
|
8 |
|
9 |
+
@app.route('/health')
|
10 |
+
def health():
|
11 |
+
return "OK", 200
|
12 |
+
|
13 |
if __name__ == '__main__':
|
14 |
+
app.run(host='0.0.0.0', port=6000, threaded=True)
|
nginx.conf
CHANGED
@@ -1,17 +1,37 @@
|
|
|
|
|
|
|
|
1 |
events {
|
2 |
worker_connections 1024;
|
3 |
}
|
4 |
|
5 |
http {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
server {
|
7 |
listen 7860;
|
8 |
|
9 |
location /flask {
|
10 |
proxy_pass http://127.0.0.1:6000;
|
|
|
|
|
11 |
}
|
12 |
|
13 |
location /node {
|
14 |
proxy_pass http://127.0.0.1:7000;
|
|
|
|
|
15 |
}
|
16 |
}
|
17 |
}
|
|
|
1 |
+
worker_processes auto;
|
2 |
+
pid /tmp/nginx.pid;
|
3 |
+
|
4 |
events {
|
5 |
worker_connections 1024;
|
6 |
}
|
7 |
|
8 |
http {
|
9 |
+
client_body_temp_path /tmp/client_temp;
|
10 |
+
proxy_temp_path /tmp/proxy_temp_path;
|
11 |
+
fastcgi_temp_path /tmp/fastcgi_temp;
|
12 |
+
uwsgi_temp_path /tmp/uwsgi_temp;
|
13 |
+
scgi_temp_path /tmp/scgi_temp;
|
14 |
+
|
15 |
+
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
16 |
+
'$status $body_bytes_sent "$http_referer" '
|
17 |
+
'"$http_user_agent" "$http_x_forwarded_for"';
|
18 |
+
|
19 |
+
access_log /tmp/nginx_access.log main;
|
20 |
+
error_log /tmp/nginx_error.log;
|
21 |
+
|
22 |
server {
|
23 |
listen 7860;
|
24 |
|
25 |
location /flask {
|
26 |
proxy_pass http://127.0.0.1:6000;
|
27 |
+
proxy_set_header Host $host;
|
28 |
+
proxy_set_header X-Real-IP $remote_addr;
|
29 |
}
|
30 |
|
31 |
location /node {
|
32 |
proxy_pass http://127.0.0.1:7000;
|
33 |
+
proxy_set_header Host $host;
|
34 |
+
proxy_set_header X-Real-IP $remote_addr;
|
35 |
}
|
36 |
}
|
37 |
}
|
server.js
CHANGED
@@ -6,6 +6,20 @@ app.get('/', (req, res) => {
|
|
6 |
res.send('Hello from Node.js!');
|
7 |
});
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
app.listen(port, '0.0.0.0', () => {
|
10 |
console.log(`Node.js server listening at http://0.0.0.0:${port}`);
|
|
|
|
|
|
|
|
|
|
|
11 |
});
|
|
|
6 |
res.send('Hello from Node.js!');
|
7 |
});
|
8 |
|
9 |
+
app.get('/health', (req, res) => {
|
10 |
+
res.status(200).send('OK');
|
11 |
+
});
|
12 |
+
|
13 |
+
app.use((err, req, res, next) => {
|
14 |
+
console.error(err.stack);
|
15 |
+
res.status(500).send('Something broke!');
|
16 |
+
});
|
17 |
+
|
18 |
app.listen(port, '0.0.0.0', () => {
|
19 |
console.log(`Node.js server listening at http://0.0.0.0:${port}`);
|
20 |
+
});
|
21 |
+
|
22 |
+
process.on('uncaughtException', (err) => {
|
23 |
+
console.error('Uncaught Exception:', err);
|
24 |
+
process.exit(1);
|
25 |
});
|