nginx如何配多域名
温馨提示:这篇文章已超过147天没有更新,请注意相关的内容是否还可用!
🌐 Nginx 配置多域名详解 🌐
在网站运维过程中,多域名配置是一个常见的需求,Nginx 作为一款高性能的Web服务器,可以轻松实现多域名的配置,本文将详细介绍如何在 Nginx 中配置多域名。
我们需要了解 Nginx 的基本配置结构,Nginx 的配置文件通常位于
/etc/nginx/nginx.conf或
/etc/nginx/sites-available/目录下,以下是一个简单的 Nginx 配置文件示例:
目录下,以下是一个简单的 Nginx 配置文件示例:
user nginx;worker_processes auto;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 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } }}我们以配置两个域名为例,讲解如何在 Nginx 中实现多域名。
创建新的 Nginx 配置文件:在
/etc/nginx/sites-available/目录下创建一个新的配置文件,
example.com.conf。
。
添加域名配置:在
example.com.conf文件中,添加以下配置:
文件中,添加以下配置:
server { listen 80; server_name example.com www.example.com; location / { root /var/www/example.com; index index.html index.htm; }}- 创建软链接:将新创建的配置文件链接到
/etc/nginx/sites-enabled/目录下,以便 Nginx 识别。
- 目录下,以便 Nginx 识别。
- 重启 Nginx:重新加载 Nginx 配置,使更改生效。
- 重启 Nginx:重新加载 Nginx 配置,使 HTTPS 配置生效。
ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/
systemctl restart nginx
当您访问
example.com或
www.example.com时,都会显示
/var/www/example.com目录下的内容。
目录下的内容。
如果您需要配置 HTTPS,可以使用以下步骤:
获取 SSL 证书:您可以从 Let's Encrypt 或其他证书颁发机构获取免费的 SSL 证书。
修改 Nginx 配置:在
example.com.conf文件中,添加以下配置:
文件中,添加以下配置:
server { listen 443 ssl; server_name example.com www.example.com; ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; ssl_session_cache shared:SSL:1m; ssl_session_timeout 10m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { root /var/www/example.com; index index.html index.htm; }}systemctl restart nginx
至此,您已成功在 Nginx 中配置了多域名,希望本文对您有所帮助!🎉
The End
发布于:2025-06-14,除非注明,否则均为原创文章,转载请注明出处。