nginx简单实现转发
时间: 2023-10-11 17:12:10 浏览: 76
nginx可以通过配置文件中的stream模块来实现转发。首先,打开nginx的配置文件,可以使用sudo gedit /etc/nginx/nginx.conf命令来编辑。
在配置文件中,添加stream模块的配置,可以参考以下示例。示例中,我们将53端口和80端口的请求转发到其他端口进行服务。你也可以根据需要进行修改。
```
stream {
log_format proxy '$remote_addr [$time_local '
'$protocol $status $bytes_sent $bytes_received '
'$session_time "$upstream_addr" '
'"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
access_log /var/log/nginx/access.log proxy;
open_log_file_cache off;
upstream dns_proxy {
server 127.0.0.1:8053;
}
upstream hello_proxy {
server 127.0.0.1:8000;
}
server {
listen 53 udp reuseport;
proxy_pass dns_proxy;
}
server {
listen 8054;
proxy_connect_timeout 1s;
proxy_timeout 300s;
proxy_pass hello_proxy;
}
}
```
配置完成后,保存文件并重启nginx服务,可以使用sudo nginx -s reload命令来重启。
这样,nginx就可以简单地实现转发功能了。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [使用nginx实现四层负载均衡(利用nginx实现请求转发)](https://blog.csdn.net/weixin_44358770/article/details/125065779)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [nginx实现请求转发](https://download.csdn.net/download/weixin_38609128/12900744)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文