Docker中使用Nginx代理多个应用站点的方法

2020-06-17 07:22:21易采站长站整理

nginx 的 Dockerfile

这个文件可以把前面的那个直接拿来,然后加上关于 php 相关的就行了。


FROM nginx:alpine

COPY nginx.conf /etc/nginx/

RUN apk update
&& apk upgrade
&& apk --update add logrotate
&& apk add --no-cache openssl
&& apk add --no-cache bash

RUN set -x ;
addgroup -g 82 -S www-data ;
adduser -u 82 -D -S -G www-data www-data && exit 0 ; exit 1

ARG PHP_UPSTREAM_CONTAINER=php-fpm
ARG PHP_UPSTREAM_PORT=9000

# Set upstream conf and remove the default conf
RUN echo "upstream php-upstream { server ${PHP_UPSTREAM_CONTAINER}:${PHP_UPSTREAM_PORT}; }" > /etc/nginx/conf.d/upstream.conf
&& rm /etc/nginx/conf.d/default.conf

ADD ./startup.sh /opt/startup.sh
RUN sed -i 's/.//g' /opt/startup.sh

CMD ["/bin/bash", "/opt/startup.sh"]

EXPOSE 80 443

php-fpm 的 Dockerfile


FROM php:7.3-fpm

ARG PUID=1000

ENV PUID ${PUID}

ARG PGID=1000

ENV PGID ${PGID}

RUN groupmod -o -g ${PGID} www-data &&
usermod -o -u ${PUID} -g www-data www-data

EXPOSE 9000

WORKDIR /var/www

CMD ["php-fpm"]

别忘了 php.ini 文件,也可以使用它默认的,那就要把这个相关的配置删掉。

 服务 baipiaoquan.com.conf 的配置


server {

listen 80 default_server;

# For https
listen 443 ssl default_server;
ssl_certificate /etc/nginx/ssl/3243258_baipiaoquan.com.pem;
ssl_certificate_key /etc/nginx/ssl/3243258_baipiaoquan.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;

add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";

# localhost 一定要
server_name localhost baipiaoquan.com www.baipiaoquan.com;
root /var/www/; # 这个和前面的配置保持一致
index index.php index.html index.htm;

location / {
try_files $uri $uri/ /index.php$is_args$args;
}

location ~ .php$ {
try_files $uri /index.php =404;
fastcgi_pass php-upstream; # 这个是 nginx Dockerfile 里配置的
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fixes timeouts
fastcgi_read_timeout 600;
include fastcgi_params;
}

location ~ /.ht {
deny all;
}

location /.well-known/acme-challenge/ {
root /var/www/letsencrypt/;
log_not_found off;
}
}