前后端都用得上的 Nginx 日常使用经验

docker-compose配置文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
version: '3.1'
services:
  nginx:
    image: nginx:1.24.0
    container_name: nginx_1_24
    restart: always
    environment:
      TZ : 'Asia/Shanghai'
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./config/nginx.conf:/etc/nginx/nginx.conf
      - ./config/conf.d:/etc/nginx/conf.d
      - ./html:/usr/share/nginx/html
      - ./logs:/var/log/nginx
      - ./ssl:/ssl
      
    networks:
      - devopsnetwork

networks:
  devopsnetwork:
    external: true

创建相应的挂载目录

指定网络:devopsnetwork docker network create devopsnetwork

运行 docker compose up -d

nginx配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
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;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

自定义配置 conf.d/default.conf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
server {
  listen       80;
  listen       443 ssl;
  server_name  nginx.devops.test.com;

  ssl_certificate      /ssl/nginx.devops.test.com.crt;
  ssl_certificate_key  /ssl/nginx.devops.test.com.key;

  ssl_session_cache    shared:SSL:1m;
  ssl_session_timeout  5m;

  ssl_ciphers  HIGH:!aNULL:!MD5;
  ssl_prefer_server_ciphers  on;
  location / {
      add_header Content-Type text/plain;
      return 200 'nginx.devops.test.com 001';
  }
}

生成自定义ssl证书

参考