这篇文章给大家介绍的内容是关于什么是nginx代理服务?nginx正向代理和反向代理的实例,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。


区别在于代理的对象不一样。


Syntax: proxy_pass URL; Default: — Context: location, if in location, limit_except
URL支持:
http:http://localhost:8000/uri/
https:https://192.168.1.111:8000/uri/
socket:http://unix:/tmp/backend.socket:/uri/
vim conf.d/real_server.confserver {
# 监听8080端口
listen 8080;
location / {
# 配置访问根目录为 /vagrant/proxy
root /vagrant/proxy;
}
}vim conf.d/fx_proxy.confserver {
# 监听80端口
listen 80;
server_name localhost;
location ~ /fx_proxy.html {
# 设置反向代理,将访问 /fx_proxy.html 的请求转发到 http://127.0.0.1:8080
proxy_pass http://127.0.0.1:8080;
}
}/vagrant/proxy/fx_proxy.html 文件vim /vagrant/proxy/fx_proxy.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>反向代理</title>
</head>
<body>
<h1>反向代理</h1>
</body>
</html>ss -tln 查看 80 端口和 8080 端口全部开启[root~]# ss -tln State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:8080 *:* LISTEN 0 128 *:80 *:* LISTEN 0 128 *:22 *:* LISTEN 0 10 127.0.0.1:25 *:* LISTEN 0 128 :::22 :::*
http://127.0.0.1/fx_proxy.html可以正常访问
[root~]# curl http://127.0.0.1/fx_proxy.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>反向代理</title>
</head>
<body>
<h1>反向代理</h1>
</body>
</html>http://127.0.0.1:8080/fx_proxy.html可以正常访问
[root~]# curl http://127.0.0.1:8080/fx_proxy.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>反向代理</title>
</head>
<body>
<h1>反向代理</h1>
</body>
</html>正向代理须在有公网IP的正式的服务器上测试。
笔者远程服务器的IP地址为:39.106.178.166,测试用的域名为 zx_proxy.ws65535.top
vim conf.d/real_server.confserver {
# 监听80端口
listen 80;
# 域名为 zx_proxy.ws65535.top;
server_name zx_proxy.ws65535.top;
location / {
# $http_x_forwarded_for 可以记录客户端及所有中间代理的IP
# 判断客户端IP地址是否是 39.106.178.166,不是则返回403
if ($http_x_forwarded_for !~* "^39\.106\.178\.166") {
return 403;
}
root /usr/share/nginx/html;
index index.html;
}
}http://zx_proxy.ws65535.top/,返回 403 Forbidden,说明访问被拒绝
vim conf.d/zx_proxy.confserver {
# 代理服务监听的端口(注意,一定要看服务器供应商控制台的安全组是否开启了该端口)
listen 3389;
# 配置DNS,223.5.5.5是阿里云的DNS
resolver 223.5.5.5;
# 正向代理配置
location / {
proxy_pass http://$http_host$request_uri;
}
}控制面板 -> 网络和Internet -> 代理 -> 手动设置代理

http://zx_proxy.ws65535.top/,可以正常访问
相关文章推荐:
Nginx作为静态资源web服务来控制浏览器缓存以及实现防盗链
以上就是什么是Nginx代理服务?nginx正向代理和反向代理的实例的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号