Https 跳转到 http 后,$_SERVER['HTTP_REFERER']获取不到值的解决方案

本文探讨了从HTTPS主域名跳转至HTTP二级域名时,因浏览器安全策略导致无法获取HTTP_REFERER的问题,并提供了两种解决方案:一是通过在HTTPS页面添加Meta标签建议浏览器发送Referer;二是将全站升级为HTTPS。作者选择了第一种方法实施。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前提,一个主域名页面的链接上面,有 N 个二级域名链接。

但是要在部分二级域名中判断 URL 来路,来防止迅雷等下载工具。

在本地测试一切正常,但在线上就是不行,搜索得知 HTTPS 到 HTTP 默认是获取不到 HTTP_REFERER 这个值的。

恰恰我的主域名为 HTTPS ,二级域名全部为 HTTP。

解决方案如下:

1、Https端解决方案,在 HTTPS 主域名页面添加 meta 标签

增加一个Meta头信息,来建议浏览器发送Referer,这并不是HTTP强制要求的,不排除有个别浏览器不去这么做。

<meta content="always" name="referrer">

2、只用将全站升级成Https,否则没有办法。

 

因为前侧域名太多,我才用了第一种方法。

user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { client_max_body_size 200m; # 开启gzip压缩 gzip on; gzip_min_length 1k; gzip_buffers 16 64K; gzip_http_version 1.1; gzip_comp_level 5; gzip_types text/plain application/x-javascript text/css application/xml application/javascript; gzip_vary on; gzip_disable "MSIE [1-6]\\."; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 4096; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf; # ===================== 通用 HTTP 重定向到 HTTPS ===================== server { listen 80; server_name dd.segma-digital.com produce.segma-digital.com; # 所有二级域名 return 301 https://$host$request_uri; # 自动跳转HTTPS } # ===================== 二级域名 1: dd.segma-digital.com ===================== server { listen 443 ssl http2; # 启用 SSL 和 HTTP/2 server_name dd.segma-digital.com; # 第一个二级域名 # SSL 证书配置 ssl_certificate /etc/nginx/ssl/dd.segma-digital.com.crt; # 证书文件 ssl_certificate_key /etc/nginx/ssl/dd.segma-digital.com.key; # 私钥文件 # SSL 优化配置 ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256; ssl_prefer_server_ciphers on; ssl_stapling on; ssl_stapling_verify on; # 安全头部 add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; add_header X-Content-Type-Options nosniff; add_header X-Frame-Options SAMEORIGIN; add_header X-XSS-Protection "1; mode=block"; charset utf-8; location / { root /home/segma; try_files $uri $uri/ /index.html; index index.html index.htm; } location /prod-api/ { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # 添加协议头 proxy_pass http://localhost:8080/; } } # ===================== 二级域名 2: produce.segma-digital.com ===================== server { listen 443 ssl http2; # 启用 SSL 和 HTTP/2 server_name produce.segma-digital.com; # 第二个二级域名 # SSL 证书配置 ssl_certificate /etc/nginx/ssl/produce.segma-digital.com.crt; # 证书文件 ssl_certificate_key /etc/nginx/ssl/produce.segma-digital.com.key; # 私钥文件 # SSL 优化配置(同上) ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256; ssl_prefer_server_ciphers on; ssl_stapling on; ssl_stapling_verify on; # 安全头部 add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; add_header X-Content-Type-Options nosniff; add_header X-Frame-Options SAMEORIGIN; add_header X-XSS-Protection "1; mode=block"; charset utf-8; location / { root /home/segma1; try_files $uri $uri/ /index.html; index index.html index.htm; } location /prod-api/ { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # 添加协议头 proxy_pass http://localhost:8081/; } } }使用dd.segma-digital.com域名上传文件,上传到了8081端口的系统程序
最新发布
08-13
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; access_log logs/host.access.log main; # 开启访问日志 root "C:/nginx/nginx-1.25.1/html"; # 精确拦截/login路径的POST请求 location = /login { if ($request_method = "POST") { # 正确格式 access_log logs/login_post.log; # 记录POST请求 return 404; } # 处理其他请求方法(可选) return 405 "Method Not Allowed"; } # 错误页面配置 error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; internal; # 禁止直接访问错误页面 } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }我想让nginx代理"C:\nginx\nginx-1.25.1\html\login.html"该怎么做,就是输入localhost直接就能跳转到这个文件
03-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值