泊坞窗+ nginx + node.js + php-fpm [英] docker + nginx + node.js + php-fpm
问题描述
所有人.
我在启动Docker容器时遇到一些问题. 最后的任务是:
- 具有80和443端口的nginx作为localhost:3000的反向代理(没关系);
- 具有3000端口的node.js是前端(可以);
- 具有9000端口和domain.con/api url的php-fpm(不起作用);
因此,主要问题在于nginx.conf:
upstream app {
server app:3000;
}
server {
listen 80;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
location / {
try_files $uri @app;
}
location /api {
try_files $uri /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass api:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location @app {
proxy_pass http://app;
}
}
当我尝试进入本地主机时-没关系,node.js正常运行; 但是,当我尝试进入localhost/api时,它给我一个错误,提示找不到文件.
docker-compose看起来像:
version: '3'
services:
api:
container_name: api
build:
context: ./api
dockerfile: Dockerfile
restart: unless-stopped
volumes:
- ./api:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
ports:
- '9000:9000'
networks:
- app-network
app:
container_name: app
build:
context: ./app
dockerfile: Dockerfile
restart: always
ports:
- '3000'
volumes:
- ./app:/app
networks:
- app-network
nginx:
container_name: nginx
image: nginx:alpine
restart: unless-stopped
volumes:
- ./api:/var/www
- ./nginx/:/etc/nginx/conf.d
ports:
- '80:80'
depends_on:
- app
- api
networks:
- app-network
networks:
app-network:
driver: bridge
我已经无数次使用apache和nginx设置反向代理,而且我总是发现此活动非常耗时(不容易测试和调试). /p>
自从我开始使用docker和docker-compose以来,我发现了一种设置反向代理服务的简便得多的方法,现在可以将时间花在应用程序上了.这种简单的方法是在docker撰写文件中使用 Traefik 服务:
version: "3"
services:
reverseproxy: # see https://docs.traefik.io/#the-traefik-quickstart-using-docker
image: traefik
command: --docker
ports:
- "80:80"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
backend:
image: someapp
expose:
- 8080
labels:
traefik.frontend.rule: PathPrefixStrip:/api
traefik.port: 8080
frontend:
image: nginx
volumes:
- ./www:/usr/share/nginx/html/:ro
expose:
- 80
labels:
traefik.frontend.rule: PathPrefixStrip:/
traefik.port: 80
如您所见,所有反向代理规则都在目标容器上指定为 labels . Traefik很好地完成了反向代理工作,正确地处理了HTTP/2,websockets,转发标头,……节省了很多时间.
everyone.
I have some problems with launching Docker containers. The final task is:
- nginx with 80 and 443 ports as a reverse proxy to localhost:3000 (it's ok);
- node.js with 3000 port is frontend (it's ok);
- php-fpm with 9000 port and domain.con/api url (it's not working);
So, the main problem is with nginx.conf:
upstream app {
server app:3000;
}
server {
listen 80;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
location / {
try_files $uri @app;
}
location /api {
try_files $uri /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass api:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location @app {
proxy_pass http://app;
}
}
When I try to get to localhost — it's ok, node.js is working fine; But when I try to get to localhost/api — it gives me an error that file not found.
docker-compose looks like:
version: '3'
services:
api:
container_name: api
build:
context: ./api
dockerfile: Dockerfile
restart: unless-stopped
volumes:
- ./api:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
ports:
- '9000:9000'
networks:
- app-network
app:
container_name: app
build:
context: ./app
dockerfile: Dockerfile
restart: always
ports:
- '3000'
volumes:
- ./app:/app
networks:
- app-network
nginx:
container_name: nginx
image: nginx:alpine
restart: unless-stopped
volumes:
- ./api:/var/www
- ./nginx/:/etc/nginx/conf.d
ports:
- '80:80'
depends_on:
- app
- api
networks:
- app-network
networks:
app-network:
driver: bridge
I've been setting up reverse proxies with apache and nginx on numerous occasions and I always found this activity time consuming (not easy to test and debug).
Since I started to work with docker and docker-compose, I found a much easier way to setup a reverse proxy service and now can spend my time on the apps. This easy way is to make use of a Traefik service in your docker compose file:
version: "3"
services:
reverseproxy: # see https://docs.traefik.io/#the-traefik-quickstart-using-docker
image: traefik
command: --docker
ports:
- "80:80"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
backend:
image: someapp
expose:
- 8080
labels:
traefik.frontend.rule: PathPrefixStrip:/api
traefik.port: 8080
frontend:
image: nginx
volumes:
- ./www:/usr/share/nginx/html/:ro
expose:
- 80
labels:
traefik.frontend.rule: PathPrefixStrip:/
traefik.port: 80
As you can see, all reverse proxy rules are specified as labels on target containers. Traefik does the reverse proxy job quite well, handling correctly HTTP/2, websockets, forwarding headers, ... It's quite a time saver.
这篇关于泊坞窗+ nginx + node.js + php-fpm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!