nginx绑定多个域名

时间:2023-03-09 09:56:13
nginx绑定多个域名

nginx绑定多个域名涉及到的技术为url rewrite,可以先了解下知识背景再过来学习。

这里以域名:www.sample.com为例

1.在/usr/local/nginx/conf文件夹中创建sample.conf文件

2.在其中写入如下内容并保存:

server{
listen 80;
server_name sample.cn www.sample.cn;
root /home/www/sample;
index index.html index.htm;
charset utf-8;
location / {
root /home/www/sample;
index index.html index.htm;
} location ~* \.(jpg|gif|png)$ {
if (-f $request_filename) {
expires max;
break;
}
}
location ~ /\.ht {
deny all;
}
}

3.然后打开nginx.conf文件,在最后一行追加如下内容:

    include linkcp.conf;

4.创建/home/www/sample文件夹,并在其中创建文件index.html内容如下

<html>
<head>
</head>
<body>
<p>Test Page!</p>
</body>
</html>

5.在浏览器中输入域名www.sample.com显示如下:

Test Page!

配置完成