如何在nginx配置文件中正确设置虚拟主机?
- 虚拟主机
- 2025-11-07
- 5

在Nginx中配置虚拟主机(也称为虚拟服务器)可以让我们在同一台服务器上运行多个网站,每个网站都有独立的域名和配置,以下是Nginx下虚拟主机配置文件的基本步骤和示例。


虚拟主机配置步骤
- 准备域名:确保你有一个域名,并且该域名已解析到服务器的IP地址。
- 创建配置文件:在Nginx的配置目录下创建一个新的配置文件。
- 设置监听端口:在配置文件中设置监听端口,通常是80(HTTP)或443(HTTPS)。
- 配置服务器块:在配置文件中添加一个新的服务器块,用于指定虚拟主机的基本信息。
- 配置根目录:指定虚拟主机的根目录,这是网站内容的存放位置。
- 配置其他设置:根据需要配置其他设置,如错误日志、访问日志、索引选项等。
- 重启Nginx:保存配置文件并重启Nginx以应用更改。
示例配置
以下是一个简单的虚拟主机配置文件示例:
server { listen 80; server_name example.com www.example.com; root /usr/share/nginx/html/example.com; index index.html index.htm; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location / { try_files $uri $uri/ =404; } location ~* .(jpg|jpeg|gif|png|bmp|swf)$ { expires max; add_header CacheControl "public"; } location ~* .(css|js|txt|xml)$ { expires 1d; add_header CacheControl "public"; } location ~* .(ico)$ { expires 1; add_header CacheControl "public"; } location ~* .(pdf)$ { expires 1M; add_header CacheControl "public"; } location ~* .(doc|docx)$ { expires 1M; add_header CacheControl "public"; } location ~* .(xls|xlsx)$ { expires 1M; add_header CacheControl "public"; } location ~* .(ppt|pptx)$ { expires 1M; add_header CacheControl "public"; } location ~* .(mp3|mp4|ogg|wav)$ { expires 1; add_header CacheControl "public"; } location ~* .(zip|rar|tar|gz|7z|iso)$ { expires 1M; add_header CacheControl "public"; } location ~* .(flv)$ { expires 1; add_header CacheControl "public"; } location ~* .(txt)$ { expires 1; add_header CacheControl "public"; } location ~* .(xml)$ { expires 1; add_header CacheControl "public"; } location ~* .(html|htm)$ { expires 1; add_header CacheControl "public"; } location ~* .(json)$ { expires 1; add_header CacheControl "public"; } location ~* .(rss)$ { expires 1; add_header CacheControl "public"; } location ~* .(atom)$ { expires 1; add_header CacheControl "public"; } location ~* .(mobi)$ { expires 1; add_header CacheControl "public"; } location ~* .(jar)$ { expires 1; add_header CacheControl "public"; } location ~* .(war)$ { expires 1; add_header CacheControl "public"; } location ~* .(sh)$ { expires 1; add_header CacheControl "public"; } location ~* .(sql)$ { expires 1; add_header CacheControl "public"; } location ~* .(log)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.gz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.bz2)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.lz4)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.xz)$ { expires 1; add_header CacheControl "public"; } location ~* .(tar.zst)$ { expires 1; add_header CacheControl "