,1. **安装Nginx**:确保服务器上已安装Nginx。,2. **创建配置文件**:在Nginx的配置目录(通常是/etc/nginx/conf.d/
或/etc/nginx/sites-available/
)中创建一个新的配置文件,如example.com.conf
。,3. **编写虚拟主机配置**:在新创建的配置文件中定义域名和IP地址。, ``, server {, listen 80;, server_name example.com;, , location / {, proxy_pass http://backend.example.com;, }, },
`,4. **启用虚拟主机**:将新配置文件链接到
sites-enabled目录,然后重启Nginx服务。,5. **设置IP定向**:在配置文件中指定需要指向的IP地址,,
`, server {, listen 80;, server_name example.com;, , if ($remote_addr != "192.0.2.1") {, return 444; # 返回HTTP 444错误代码, }, , location / {, proxy_pass http://backend.example.com;, }, },
`,6. **测试配置**:在重新启动Nginx前,使用
nginx -t命令测试配置文件的语法是否正确。,7. **重启Nginx**:执行
sudo systemctl restart nginx`来应用更改。,通过以上步骤,你可以配置Nginx使用特定IP地址的请求转发到指定后端服务器。.