Nginx的主要配置文件通常位于/etc/nginx/nginx.conf
或/etc/nginx/conf.d/default.conf
。我们可以通过文本编辑器(如nano、vim等)打开并编辑该文件。以下是具体步骤:
原始配置示例
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}
修改后的配置示例
配置项 | 描述 |
---|---|
listen |
监听端口,默认为80,即HTTP协议的标准端口。 |
server_name |
设置为您的域名,用于匹配来自浏览器的请求。 |
location / |
定义了当访问根路径时的行为。 |
proxy_pass |
指定实际提供服务的目标服务器地址,包括协议、IP地址和端口号。 |
proxy_set_header Host $http_host |
将原始请求中的Host头转发给目标服务器,保持URL的一致性。 |
proxy_set_header X-Forward-For $remote_addr |
记录客户端的真实IP地址,便于日志记录和安全审计。 |
server {
listen 80;
server_name 您的域名;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://目标服务器IP:端口号;
proxy_set_header Host $http_host;
proxy_set_header X-Forward-For $remote_addr;
index index.html index.htm;
}
}