• Index

nginx配置多个域名

Last updated: ... / Reads: 127 Edit

当配置多个域名时,以下是一个示例配置的简单步骤:

  1. 打开 Nginx 的主配置文件。在 Ubuntu 上,该文件通常位于 /etc/nginx/nginx.conf

  2. http 块内,添加每个域名的虚拟主机配置。例如:

http {
    # ...其他配置...

    server {
        listen 80;
        server_name example.com;

        location / {
            root /var/www/example.com;
            index index.html;
        }
    }

    server {
        listen 80;
        server_name anotherdomain.com;

        location / {
            root /var/www/anotherdomain.com;
            index index.html;
        }
    }
}

上述示例配置了两个域名:example.comanotherdomain.com。每个 server 块指定了域名的监听端口、根目录以及默认文件。

  1. 如果需要启用 HTTPS,请在相应的 server 块内配置 SSL 证书和私钥路径:
server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /path/to/example.com.crt;
    ssl_certificate_key /path/to/example.com.key;

    location / {
        root /var/www/example.com;
        index index.html;
    }
}

这是一个示例配置,假设您已经有了名为 example.com.crtexample.com.key 的 SSL 证书和私钥文件。

  1. 根据您的实际需求和服务器设置,进一步调整每个域名的配置,例如添加其他 location 块来处理不同的 URL 路径或使用其他 Nginx 功能。

  2. 保存配置文件并重新加载 Nginx 配置,以使更改生效。在 Ubuntu 上,可以使用命令 sudo service nginx reload

请根据您的特定域名、路径和证书信息进行调整,这个示例提供了一个基本框架,供您参考和修改。


Comments

Make a comment

  • Index