-
-
- 安装Nginx组件;
- 配置文件名为proxy.conf,放置在/etc/nginx/conf.d/目录下;
- 为www.chinaskills.cn配置代理前端,通过HTTPS的访问访问后端Web服务器;
- 后端服务器日志内容需要记录真实客户端的IP地址。
- 缓存后端Web服务器上的静态页面。
-
proxy_pass 代理走向
假设下面四种情况分别用 http://192.168.1.1/proxy/test.html 进行访问。
第一种:
location /proxy/ {
proxy_pass http://127.0.0.1/;
}
代理到URL:http://127.0.0.1/test.html
第二种(相对于第一种,最后少一个 / )
location /proxy/ {
proxy_pass http://127.0.0.1;
}
代理到URL:http://127.0.0.1/proxy/test.html
第三种:
location /proxy/ {
proxy_pass http://127.0.0.1/aaa/;
}
代理到URL:http://127.0.0.1/aaa/test.html
第四种(相对于第三种,最后少一个 / )
location /proxy/ {
proxy_pass http://127.0.0.1/aaa;
}
代理到URL:http://127.0.0.1/aaatest.html
两个proxy_pass
的关系和区别
在两个模块中,两个proxy_pass
都是用来做后端代理的指令。ngx_stream_proxy_module
模块的proxy_pass
指令只能在server段使用使用, 只需要提供域名或ip地址和端口。可以理解为端口转发,可以是tcp端口,也可以是udp端口。ngx_http_proxy_module
模块的proxy_pass
指令需要在location段,location中的if段,limit_except段中使用,处理需要提供域名或ip地址和端口外,还需要提供协议,如"http"或"https",还有一个可选的uri可以配置。
ngx_http_proxy_module
模块的proxy_pass
指令
server {
listen 80;
server_name www.test.com;
# 正常代理,不修改后端url的
location /some/path/ {
proxy_pass http://127.0.0.1;
}
# 修改后端url地址的代理(本例后端地址中,最后带了一个斜线)
location /testb {
proxy_pass http://www.other.com:8801/;
}
# 使用 if in location
location /google {
if ( $geoip_country_code ~ (RU|CN) ) {
proxy_pass http://www.google.hk;
}
}
location /yongfu/ {
# 没有匹配 limit_except 的,代理到 unix:/tmp/backend.socket:/uri/
proxy_pass http://unix:/tmp/backend.socket:/uri/;;
# 匹配到请求方法为: PUT or DELETE, 代理到9080
limit_except PUT DELETE {
proxy_pass http://127.0.0.1:9080;
}
}
}
proxy_pass
后,后端服务器的url
(request_uri
)情况分析
server {
listen 80;
server_name www.test.com;
# 情形A
# 访问 http://www.test.com/testa/aaaa
# 后端的request_uri为: /testa/aaaa
location ^~ /testa/ {
proxy_pass http://127.0.0.1:8801;
}
# 情形B
# 访问 http://www.test.com/testb/bbbb
# 后端的request_uri为: /bbbb
location ^~ /testb/ {
proxy_pass http://127.0.0.1:8801/;
}
# 情形C
# 下面这段location是正确的
location ~ /testc {
proxy_pass http://127.0.0.1:8801;
}
# 情形D
# 下面这段location是错误的
#
# nginx -t 时,会报如下错误:
#
# nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular
# expression, or inside named location, or inside "if" statement, or inside
# "limit_except" block in /opt/app/nginx/conf/vhost/test.conf:17
#
# 当location为正则表达式时,proxy_pass 不能包含URI部分。本例中包含了"/"
location ~ /testd {
proxy_pass http://127.0.0.1:8801/; # 记住,location为正则表达式时,不能这样写!!!
}
# 情形E
# 访问 http://www.test.com/ccc/bbbb
# 后端的request_uri为: /aaa/ccc/bbbb
location /ccc/ {
proxy_pass http://127.0.0.1:8801/aaa$request_uri;
}
# 情形F
# 访问 http://www.test.com/namea/ddd
# 后端的request_uri为: /yongfu?namea=ddd
location /namea/ {
rewrite /namea/([^/]+) /yongfu?namea=$1 break;
proxy_pass http://127.0.0.1:8801;
}
# 情形G
# 访问 http://www.test.com/nameb/eee
# 后端的request_uri为: /yongfu?nameb=eee
location /nameb/ {
rewrite /nameb/([^/]+) /yongfu?nameb=$1 break;
proxy_pass http://127.0.0.1:8801/;
}
access_log /data/logs/www/www.test.com.log;
}
server {
listen 8801;
server_name www.test.com;
root /data/www/test;
index index.php index.html;
rewrite ^(.*)$ /test.php?u=$1 last;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
access_log /data/logs/www/www.test.com.8801.log;
}
nginx中$host、$http_host和$proxy_host区别
变量 | 是否显示端口 | 值 |
---|---|---|
$host | 不显示端口 | 浏览器请求的ip,不显示端口 |
$http_host | 端口存在则显示 | 浏览器请求的ip和端口号 |
$proxy_host | 默认80端口不显示,其它显示 | 被代理服务的ip和端口号 |
主要:
Nginx 缓存配置 - 看一百次夜空里的深蓝 - 博客园 (cnblogs.com)
nginx中$host、$http_host和$proxy_host区别_涵一的博客-CSDN博客_$host
备用
nginx proxy_cache缓存详解 - archer-wong - 博客园 (cnblogs.com)
proxy_cache_path /proxy/cache levels=1:2 keys_zone=one:20m max_size=5g inactive=1h;
server {
listen 80;
root /proxy;
index index.html;
server_name www.chinaskills.cn;
#return 301 https://www.chinaskills.cn;
location / {
proxy_pass https://81.6.63.254;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 443;
server_name www.chinaskills.cn;
root /proxy;
index index.html;
access_log /proxy/log/access.log main;
error_log /proxy/log/error.log;
ssl on;
ssl_certificate "/CA/proxy.crt";
ssl_certificate_key "/CA/proxy.pem";
location / {
# return 301 https://www.chinaskills.cn$uri;
proxy_cache one;
proxy_cache_key $host;
proxy_cache_valid any 10m;
}
location .\php$ {
fastcgi_pass unix:/run/php-fpm/php-fpm.pid;
include fastcgi.conf;
include fastcgi_params;
fastcgi_index index.php;
}
}
注释:
proxy_key
-
以'域名'、'URI'、'参数组合'成Web缓存的Key值,Nginx根据'Key值哈希',存储'缓存内容'到'二级缓存目录'内