php $_SERVER[‘REQUEST_SCHEME‘] 获取不到值

在部署PHP开源系统时遇到URL缺少HTTP协议的问题,发现$_SERVER数组中没有REQUEST_SCHEME字段。尝试在Nginx配置中添加proxy_set_header无效,最后通过修改PHP代码来判断HTTPS和服务器端口来设置REQUEST_SCHEME。对于IIS环境,$_SERVER[REQUEST_URI]为空,需使用$_SERVER[QUERY_STRING]代替。建议官方能更好地支持不同服务器环境。

今天在部署一套php开源系统,发现生成的 url 少了 http 所以跟了一遍代码发现 $_SERVER 中既然没有REQUEST_SCHEME字段,

后来尝试在 nginx 配置文件中加入以下代码无效

  1. proxy_set_header X-Forwarded-Proto  $scheme;  

复制代码



无奈只能通过修改程序解决

  1. if ( (! empty($_SERVER['REQUEST_SCHEME']) && $_SERVER['REQUEST_SCHEME'] == 'https') || (! empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (! empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443') ) {
  2.     $_SERVER['REQUEST_SCHEME'] = 'https';
  3. } else {
  4.     $_SERVER['REQUEST_SCHEME'] = 'http';
  5. }

复制代码


解决了之后查到还有这个方法,不过没有尝试,如果遇到同样问题的同学可以尝试下
 

  1. fastcgi_param REQUEST_SCHEME $scheme.

复制代码


虽然改程序能解决,但一些开源程序还是挺头疼的,个人建议希望官方能默认支持我的环境
centos7.4 + bt5.3.0 + Nginx -Tengine2.2

php $_SERVER['REQUEST_SCHEME'] 获取不到值 - Linux面板 - 宝塔面板论坛

环境windows 2003+iis6

$_SERVER['REQUEST_URI'] 怎么测试都是空值,网上查了查很纠结的结果,在此记录一下

$_SERVER['REQUEST_URI']在iis下就是空的,只有apache下才有值,解决方法 用$_SERVER['QUERY_STRING']

替换吧。
 

<?php
// 说明:获取 _SERVER['REQUEST_URI'] 值的通用解决方案
function request_uri()
{
if (isset($_SERVER['REQUEST_URI']))
{
$uri = $_SERVER['REQUEST_URI'];
}
else
{
if (isset($_SERVER['argv']))
{
$uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['argv'][0];
}
else
{
$uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['QUERY_STRING'];
}
}
return $uri;
}
?>

 

upstream bg { server 10.105.46.180:20009 weight=1 max_fails=2 fail_timeout=10s; server 10.105.252.42:20009 weight=1 max_fails=2 fail_timeout=10s; } server { listen 80; listen 8000; server_name bg.weixin12315.com; root html; index index.html index.htm index.php; error_log /data/logs/bg.weixin12315.com.err.log; access_log /data/logs/bg.weixin12315.com.acc.log access; location / { #Proxy Settings proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-SourceUrl $scheme://$host$request_uri; proxy_set_header X-TestFlag $http_TestFlag; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_cache cache_one; proxy_max_temp_file_size 0; proxy_connect_timeout 300s; proxy_send_timeout 300s; proxy_read_timeout 300s; proxy_buffer_size 64k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; if ($cookie_TestFlag = "graytest"){ proxy_pass http://10.105.220.122:30041; } if ($http_TestFlag = "graytest"){ proxy_pass http://10.105.220.122:30041; } if ($cookie_TestFlag = "graytest111"){ proxy_pass http://10.105.220.122:31041; } proxy_pass http://bg; } } 这个配置有没有问题,或者可优化的项
08-06
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; error_log logs/error.log debug; #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 18080; server_name 10.251.22.122; #charset koi8-r; #access_log logs/host.access.log main; location / { return 404; } location /sso/ { proxy_pass http://10.251.22.122:8700/sso/; proxy_set_header Host $ host; proxy_set_header X-Real-IP $ remote_addr; proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $ scheme; } location ~ /web/ { proxy_pass http://10.251.22.248:80/; proxy_set_header Host $ host; proxy_set_header X-Real-IP $ remote_addr; proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $ scheme; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # 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; # } #} } nigix配置报错invalid number of arguments in "proxy_set_header" directive in E:\nginx-t/conf/nginx.conf:50
06-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值