【Linux】Nginx:容器化便捷部署网页
以下是关于使用 Docker 部署 Nginx 的详细长文教程,包括安装、运行、配置等步骤。
·
以下是关于使用 Docker 部署 Nginx 的详细长文教程,包括安装、运行、配置等步骤。
环境准备
安装 Docker
如果尚未安装 Docker,可以参考以下简要步骤:
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
拉取 Nginx 镜像
Docker 提供了官方的 Nginx 镜像。可以通过以下命令拉取最新版本:
docker pull nginx:latest
基础运行 Nginx 容器
快速启动 Nginx
运行以下命令启动 Nginx 容器:
docker run -d \
--name nginx-container \
-p 80:80 \
nginx:latest
访问测试
- 打开浏览器访问
http://<服务器IP>(本地为http://localhost)。 - 如果能看到 “Welcome to Nginx” 页面,说明 Nginx 已成功运行。
自定义 Nginx 配置
为了实现更灵活的配置,可以挂载自定义配置文件和网站目录到 Nginx 容器中。
创建目录结构
mkdir -p ~/nginx/{conf,html,logs}
conf:存放 Nginx 配置文件。html:存放静态网页文件。logs:存放 Nginx 日志。
准备配置文件
创建配置文件 ~/nginx/conf/nginx.conf,以下是示例内容:
user nginx;
worker_processes auto;
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;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
准备静态网页
在 ~/nginx/html 目录下创建一个简单的 index.html 文件:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Custom Nginx!</title>
</head>
<body>
<h1>Welcome to Dockerized Nginx</h1>
<p>This is a custom Nginx page served from Docker.</p>
</body>
</html>
运行自定义 Nginx 容器
挂载自定义配置文件、网页和日志目录运行 Nginx 容器:
docker run -d \
--name custom-nginx \
-p 80:80 \
-v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro \
-v ~/nginx/html:/usr/share/nginx/html:ro \
-v ~/nginx/logs:/var/log/nginx \
nginx:latest
-v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro:将本地的配置文件挂载到容器。-v ~/nginx/html:/usr/share/nginx/html:ro:挂载网页文件目录到容器。-v ~/nginx/logs:/var/log/nginx:挂载日志目录到容器,方便调试。
查看日志
Nginx 的日志可以从挂载的日志目录查看:
tail -f ~/nginx/logs/access.log
tail -f ~/nginx/logs/error.log
测试和调试
- 打开浏览器访问
http://<服务器IP>或http://localhost。 - 确保看到自定义的网页内容。
高级配置
添加 HTTPS 支持
可以通过挂载 SSL 证书和密钥文件配置 HTTPS。
修改 nginx.conf,添加 HTTPS 配置:
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
location / {
root /usr/share/nginx/html;
index index.html;
}
}
运行时挂载 SSL 文件:
docker run -d \
--name nginx-https \
-p 443:443 \
-v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro \
-v ~/nginx/html:/usr/share/nginx/html:ro \
-v ~/nginx/logs:/var/log/nginx \
-v ~/nginx/ssl:/etc/nginx/ssl \
nginx:latest
反向代理
修改 nginx.conf 配置反向代理:
server {
listen 80;
server_name myapp.local;
location / {
proxy_pass http://backend-server:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
将 proxy_pass 指向后端服务地址。
管理容器
查看正在运行的容器
docker ps
重启 Nginx 容器
docker restart custom-nginx
停止和删除容器
docker stop custom-nginx
docker rm custom-nginx
总结
通过以上步骤,您可以成功使用 Docker 部署和配置 Nginx,并根据需求调整配置。
更多推荐
所有评论(0)