Linux 下nginx 配置/绑定域名

1. 配置文件(/etc/nginx/conf.d/**.conf)

  • 为每一个域名建立一个单独的配置文件时输入以下内容:
server
{
    listen   80;                            #监听端口设为 80。
    server_name  www.hlz.space;         #绑定您的域名。
    index index.htm index.html index.php;   #指定默认文件。
    root /var/www/html/hlzspace;           #指定网站根目录。
    include location.conf;                  #当您需要调用其他配置文件时才粘贴此项,如无需要,请删除此项。
}
  • 将多个域名规则写进一个共同的配置文件时输入以下内容:
server
{
    listen   80;                            #监听端口设为 80。
    server_name  www.hlz.space;         #绑定您的域名。
    index index.htm index.html index.php;   #指定默认文件。
    root /var/www/html/hlzspace;           #指定网站根目录。
    include location.conf;                  #当您需要调用其他配置文件时才粘贴此项,如无需要,请删除此项。
}
server
{
    listen   80;                            #监听端口设为 80。
    server_name  www.hlz2.space;         #绑定您的域名。
    index index.htm index.html index.php;   #指定默认文件。
    root /var/www/html/hlz2space;           #指定网站根目录。
    include location.conf;                  #当您需要调用其他配置文件时才粘贴此项,如无需要,请删除此项。
}
  • 为无 WWW 前缀的域名配置规则并加 301 跳转时输入以下内容:
server
{
    listen 80;
    server_name hlz.space;
    rewrite ^/(.*) http://www.hlz.space/$1 permanent;
}
  • 需要为域名添加 404 提示时输入以下内容:
server
{
    listen   80;                            #监听端口设为 80。
    server_name  www.hlz.space;         #绑定您的域名。
    index index.htm index.html index.php;   #指定默认文件。
    root /var/www/html/hlzspace;           #指定网站根目录。
    include location.conf;                  #当您需要调用其他配置文件时才粘贴此项,如无需要,请删除此项。
    error_page 404              #/404.html;
}

2. 重启nginx

$ service nginx  restart
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容