《使用 Docker 搭建 MySQL、WordPress、Nginx 和 Certbot 环境》文中搭建的服务器在WordPress容器中启用了Apache,如果不想启用可以选择wordpress:php8.3-fpm这类的容器,Nginx的配置文件会有些变化,nginx.conf如下
# HTTP 重定向服务器(统一处理)
server {
listen 80;
server_name example.com www.example.com;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://$host$request_uri;
}
}
# HTTPS 服务器配置
server {
listen 443 ssl http2;
server_name example.com www.example.com;
root /var/www/html; # 主目录为 /var/www/html
index index.php index.html index.htm;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
# 主目录中的 WordPress 文件
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# PHP 请求转发
location ~ \.php$ {
fastcgi_pass wordpress_example:9000; # PHP-FPM 容器名 fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# 错误页面配置
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html; # 错误页面路径
}
}