安装 nginx
安装方式:
- 服务器原生安装 nginx
- Docker
发行版本:
- ArchLinux
- Ubuntu
服务器原生安装 nginx
包管理器下载安装 nginx
ArchLinux
Ubuntu
1 2
| sudo apt update sudo apt install nginx
|
启动 nginx, 3种方式
1 2 3
| sudo systemctl start nginx sudo systemctl enable nginx sudo systemctl enable --now nginx
|
编辑 nginx 配置文件
1
| sudo vim /etc/nginx/nginx.conf
|
测试 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf;
events { worker_connections 768; }
http {
sendfile on; tcp_nopush on; types_hash_max_size 2048;
include /etc/nginx/mime.types; default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log;
gzip on;
include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; }
|
Docker
拉取 nginx 镜像
启动 nginx 镜像; 容器命名为 celiae-nginx; 只读挂载本地路径/some/content
到Docker容器路径/usr/share/nginx/html
; 只读挂载本地路径/host/path/nginx.conf
到Docker容器路径/etc/nginx/nginx.conf
; 静默启动; 映射本机8080端口到Docker容器80端口.
1
| docker run --name celiae-nginx -v /some/content:/usr/share/nginx/html:ro -v /host/path/nginx.conf:/etc/nginx/nginx.conf:ro -d -p 8080:80 nginx
|
/usr/share/nginx/html
前端页面部署的路径 (index.html)
/etc/nginx/nginx.conf
是 nginx 全局配置文件
docker-compose.yml
使用nginx镜像;挂载/templates
到Docker容器/etc/nginx/templates
;映射本地:容器;通过环境变量设置地址和端口.
1 2 3 4 5 6 7 8 9
| web: image: nginx volumes: - ./templates:/etc/nginx/templates ports: - "8080:80" environment: - NGINX_HOST=foobar.com - NGINX_PORT=80
|