这里写下基于docker的构建,但配置文件还是和你单独装nginx区别不大的(我只是将不同项目的server提出来而已)
下面是我构建nginx容器的目录安排
cert是放ssl证书的地方,证书是基于你在阿里后台已经给域名配置(购买)好才生效的哈
我这里将这个目录挂载到了nginx容器中的/etc/nginx/cert(如果你是直接安装的nginx就新建一个文件放进去,在server中配置就好)
conf里面是nginx中对应不同项目的server配置,我是在nginx.conf进行including包含进去
web是放html/css的地方,不同的项目都是不同的文件夹(我是把web挂载到nginx容器中的var/www中)
docker-compose.yml 是nginx的创建文件,master是docker的网络需要自己创建(创建方法我之前的文章中有),image是指定构建容器的镜像,会默认下载nginx:laster版本,80和443是http和https要反问的端口,volumes是文件挂载就是把冒号左边的文件和右边容器中的文件连接起来,这样就不用每次都去容器里面进行改写操作
version: '2'
networks:
default:
external:
name: master
services:
nginx:
image: nginx
container_name: nginx
restart: always
build: .
ports:
- "80:80"
- "443:443"
volumes:
- ./conf:/etc/nginx/conf.d
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./web:/var/www
- ./cert:/etc/nginx/cert
environment:
TZ: Asia/Shanghai
Dockerfile也是nginx的创建文件,from也指定哪个镜像最好和上面那个文件中指定镜像一致,run是构建是新增改目录用户上面提即的挂载需要,copy 将左边中证书复制到容器里面
FROM nginx
RUN mkdir -p /var/www
COPY ./cert/ /etc/nginx/cert/
EXPOSE 80 443
CMD ["nginx","-g","daemon off;"]
nginx.conf,nginx的全局配置文件,如果你直接将conf目录中的server配置复制到这个里面也是可以的,只是以后会很乱。
这个里面的参数配置可以参考https://blog.csdn.net/tjcyjd/article/details/50695922
user nginx;
#工作进程:数目。根据硬件调整,通常等于CPU数量或者2倍于CPU。
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
#进程的最大连接数。
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
keepalive_timeout 400;
client_header_timeout 10m;
client_body_timeout 10m;
client_max_body_size 512m;
client_header_buffer_size 16k;
large_client_header_buffers 4 16k;
proxy_connect_timeout 300;
proxy_read_timeout 300;
proxy_send_timeout 300;
proxy_buffer_size 64k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
sendfile on;
#tcp_nopush on;
#gzip on;
#将截图中的conf目录里的不同项目的server配置包含进来
include /etc/nginx/conf.d/*.conf;
}
conf目录中某一项目的server配置(一个项目有两个server)
server {#这个是https的访问配置
listen 443 ssl;
server_name ****-training.cert-***.com;#对应项目的域名,子域名是可以使用主域名的ssl证书的
#ssl on;
root html;
index index.html index.htm;
ssl_certificate cert/2817251__cert-map.com.pem; #证书的路径
ssl_certificate_key cert/2817251__cert-map.com.key; #证书的路径
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / { #这些是前端的反问路径
root /var/www/;
index index.html index.htm;
}
location /admin {
root /var/www/enterprise_***/;
}
location /mini {
root /var/www/enterprise_**/;
}
location /company {
root /var/www/enterprise_****/;
}
location /training-*** { #下面这些是访问服务端的配置
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
proxy_pass http://enterprise_training_**:8081; #内部反问路径,项目容器名称+容器暴露端口
}
location /training-*** { 这个是外部访问连接,nginx接受之后会转发给内部反问路径
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
proxy_pass http://enterprise_training:8080;
}
location /training-enterprise-*** {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
proxy_pass http://enterprise_training_***:8082;
}
}
server{ #这个是http访问的路径
listen 80;
server_name enterprise-training.cert-***;
location /page {
root /var/www/;
}
location /admin {
root /var/www/enterprise_***/;
}
location /mini {
root /var/www/enterprise_***/;
}
location /company {
root /var/www/enterprise_***/;
}
location /training-** {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
proxy_pass http://enterprise_***:8081;
}
location /training-admin {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
proxy_pass http://enterprise_**:8080;
}
location /training-enterprise-admin {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
proxy_pass http://enterprise_training_:8082;
}
# 跳转到HTTPS
#return 301 https://$server_name$request_uri;
}
最后就是去到nginx目录下运行docker-compose build和docker-compose up -d
就可以构建好nginx了(前提是你安装后docker和docker-compose了,安装的文章我前面都有写的)