Nginx 是一个高性能的 HTTP 和反向代理服务器,也用作邮件代理服务器。通过配置虚拟主机(Virtual Host),Nginx 可以同时处理多个域名或子域名的请求。
假设你有一个内网 IP 地址 192.168.1.100,并且你想在内网中通过域名 example.local 访问一个网站。
打开 Nginx 的主配置文件(通常位于 /etc/nginx/nginx.conf 或 /etc/nginx/sites-available/default),添加一个新的虚拟主机配置:
server {
listen 80;
server_name example.local;
root /var/www/example.local;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}创建网站根目录并添加一些示例文件:
mkdir -p /var/www/example.local
echo "<h1>Hello, Example!</h1>" > /var/www/example.local/index.html由于这是一个内网环境,你可以通过编辑主机的 hosts 文件来实现域名解析。编辑 /etc/hosts 文件(在 Windows 上是 C:\Windows\System32\drivers\etc\hosts),添加以下行:
192.168.1.100 example.local重启 Nginx 以应用配置更改:
sudo systemctl restart nginxsudo systemctl status nginxhosts 文件配置正确。/var/log/nginx/error.log)以获取更多信息。通过以上步骤,你应该能够在内网中通过域名 example.local 访问你的网站。