当人们尝试从手机浏览网站时,网站无法检测到移动版本存在问题
当我使用Apache时代码也能正常工作,这个问题只发生在使用nginx
这是我使用桌面或移动设备检测访问者的PHP代码
.verticalCenter {
position: relative;
top: 60%;
}
这是我的nginx配置
自定义规则
if($_COOKIE['detect_mobiles'] == 1)
{
$settings['template'] = 'smartphone';
}elseif($_COOKIE['detect_mobiles'] == 2)
{
}else{
if(preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"])){
$settings['template'] = 'smartphone';
setcookie('detect_mobiles','1',time()+(60200),'/');
setcookie('detect_mobiles2','3',time()+(60200),'/');
}else
{
setcookie('detect_mobiles','2',time()+(60200),'/');
}
}
Default.conf
set $PROXY_DOMAIN_OR_IP "192.168.1.1";
proxy_params_common
server {
listen 80 default_server;
#listen [::]:80 default_server; # Uncomment if your server supports IPv6
server_name localhost;
# deny all; # DO NOT REMOVE OR CHANGE THIS LINE - Used when Engintron is disabled to block Nginx from becoming an open proxy
set $PROXY_DOMAIN_OR_IP $host;
# Set custom rules like domain/IP exclusions or redirects here
include custom_rules;
location / {
try_files $uri @backend;
}
location @backend {
include proxy_params_common;
# === MICRO CACHING ===
# Comment the following line to disable 1 second micro-caching for dynamic HTML content
include proxy_params_dynamic;
}
# Enable browser cache for static content files (TTL is 1 hour)
location ~* \.(?:json|xml|rss|atom)$ {
include proxy_params_common;
include proxy_params_static;
expires 60s;
add_header Pragma "public";
add_header Cache-Control "public";
add_header Vary "Accept-Encoding";
access_log off;
}
# Enable browser cache for CSS / JS (TTL is 30 days)
location ~* \.(?:css|js)$ {
include proxy_params_common;
include proxy_params_static;
expires 60s;
add_header Pragma "public";
add_header Cache-Control "public";
add_header Vary "Accept-Encoding";
access_log off;
}
# Enable browser cache for media & document files (TTL is 60 days)
location ~* \.(?:ico|jpg|jpeg|gif|png|bmp|webp|tiff|mp3|flac|ogg|mid|midi|wav|wma|mp4|mov|3gp|webm|mkv|ogv|wmv|zip|7z|tgz|gz|rar|bz2|tar|exe|pdf|doc|docx|xls|xlsx|ppt|pptx|rtf|odt|ods|odp)$ {
include proxy_params_common;
include proxy_params_static;
expires 60s;
add_header Pragma "public";
add_header Cache-Control "public";
access_log off;
}
# Deny access to hidden files
location ~ /\.ht {
deny all;
}
}
proxy_params_dynamic
# General Proxy Settings
proxy_pass http://$PROXY_DOMAIN_OR_IP:8080;
proxy_http_version 1.1; # Always upgrade to HTTP/1.1
proxy_set_header Accept-Encoding ""; # Optimize encoding
proxy_set_header Connection ""; # Enable keepalives
proxy_set_header Host $host;
proxy_set_header Proxy "";
proxy_set_header Referer $http_referer;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header CF-Connecting-IP $http_cf_connecting_ip;
proxy_set_header Cf-Visitor $http_cf_visitor;
# Buffers
proxy_buffers 256 16k;
proxy_buffer_size 128k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
# Timeouts
proxy_connect_timeout 120s;
proxy_read_timeout 180s;
proxy_send_timeout 180s;
# Info
add_header X-Cache-Status $upstream_cache_status;
add_header X-Server-Powered-By "Engintron";
proxy_params_static
set $CACHE_BYPASS_FOR_DYNAMIC 0;
set $EXPIRES_FOR_DYNAMIC "1s";
# CMS (& CMS extension) specific cookies (Joomla, K2, WordPress)
if ($http_cookie ~* "joomla_[a-zA-Z0-9_]+|userID|comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $CACHE_BYPASS_FOR_DYNAMIC 1;
set $EXPIRES_FOR_DYNAMIC 0;
}
# Invision Power Board (IPB)
if ($cookie_member_id ~ "^[1-9][0-9]*$") {
set $CACHE_BYPASS_FOR_DYNAMIC 1;
set $EXPIRES_FOR_DYNAMIC 0;
}
# Admin sections & generic entry point names for CMSs
if ($request_uri ~* "/administrator|/wp-admin|/wp-login.php|/ucp.php|/login|/connect") {
set $CACHE_BYPASS_FOR_DYNAMIC 1;
set $EXPIRES_FOR_DYNAMIC 0;
}
# Generic query string to request a page bypassing Nginx's caching
if ($query_string ~* "nocache") {
set $CACHE_BYPASS_FOR_DYNAMIC 1;
set $EXPIRES_FOR_DYNAMIC 0;
}
proxy_no_cache $CACHE_BYPASS_FOR_DYNAMIC;
proxy_cache_bypass $CACHE_BYPASS_FOR_DYNAMIC;
proxy_cache engintron_dynamic;
proxy_cache_key "$request_method$scheme$host$request_uri";
proxy_cache_lock on;
proxy_cache_methods GET HEAD;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504; # Additional options: http_403 http_404
proxy_cache_valid 200 1s; # Adjust for longer cache times (unfortunately, we cannot use a variable here)
proxy_ignore_headers Expires Cache-Control Set-Cookie Vary;
# Headers
expires $EXPIRES_FOR_DYNAMIC;
add_header Pragma "public";
add_header Cache-Control "public";
add_header Vary "Accept-Encoding";
nginx.conf
proxy_cache engintron_static;
proxy_cache_key "$request_method$scheme$host$request_uri";
proxy_cache_lock on;
proxy_cache_min_uses 1;
proxy_cache_revalidate on;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504; # Additional options: http_403 http_404
proxy_cache_valid 200 301 302 1m; # Covers 200, 301 & 302 responses, caching time set to 1 minute
proxy_ignore_headers Set-Cookie;
.htaccess for main website
user nginx;
worker_processes auto;
worker_rlimit_nofile 65535;
pid /var/run/nginx.pid;
events {
worker_connections 8192;
multi_accept on;
}
http {
## Basic Settings ##
client_body_timeout 20s; # Use 5s for high-traffic sites
client_header_timeout 20s; # Use 5s for high-traffic sites
client_max_body_size 1024m;
keepalive_timeout 20s;
port_in_redirect off;
sendfile on;
server_names_hash_bucket_size 64;
server_name_in_redirect off;
server_tokens off;
tcp_nodelay on;
tcp_nopush on;
types_hash_max_size 2048;
## DNS Resolver ##
# If in China, enable the OpenDNS entry that matches your network connectivity (IPv4 only or IPv4 & IPv6)
# OpenDNS (IPv4 & IPv6)
#resolver 208.67.222.222 208.67.220.220 [2620:0:ccc::2] [2620:0:ccd::2];
# OpenDNS (IPv4 only)
#resolver 208.67.222.222 208.67.220.220;
# Google Public DNS (IPv4 & IPv6)
#resolver 8.8.8.8 8.8.4.4 [2001:4860:4860::8888] [2001:4860:4860::8844];
# Google Public DNS (IPv4 only) [default]
resolver 8.8.8.8 8.8.4.4;
# CloudFlare
# List from: https://www.cloudflare.com/ips-v4
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 104.16.0.0/12;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 131.0.72.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 199.27.128.0/21;
# List from: https://www.cloudflare.com/ips-v6
set_real_ip_from 2400:cb00::/32;
set_real_ip_from 2405:8100::/32;
set_real_ip_from 2405:b500::/32;
set_real_ip_from 2606:4700::/32;
set_real_ip_from 2803:f800::/32;
set_real_ip_from 2c0f:f248::/32;
set_real_ip_from 2a06:98c0::/29;
set_real_ip_from 192.168.1.4;
# Replace with correct visitor IP
real_ip_header X-Forwarded-For;
real_ip_recursive on;
## MIME ##
include /etc/nginx/mime.types;
default_type application/octet-stream;
## Logging Settings ##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
## Gzip Settings ##
gzip on;
gzip_buffers 16 8k;
gzip_comp_level 5;
gzip_disable "msie6";
gzip_min_length 256;
gzip_proxied any;
gzip_types
application/atom+xml
application/javascript
application/json
application/ld+json
application/manifest+json
application/rss+xml
application/vnd.geo+json
application/vnd.ms-fontobject
application/x-font-ttf
application/x-javascript
application/x-web-app-manifest+json
application/xhtml+xml
application/xml
font/opentype
image/bmp
image/svg+xml
image/x-icon
text/cache-manifest
text/css
text/javascript
text/plain
text/vcard
text/vnd.rim.location.xloc
text/vtt
text/x-component
text/x-cross-domain-policy
text/x-js
text/xml;
gzip_vary on;
# Proxy Settings
proxy_cache_path /tmp/engintron_dynamic levels=1:2 keys_zone=engintron_dynamic:20m inactive=10m max_size=500m;
proxy_cache_path /tmp/engintron_static levels=1:2 keys_zone=engintron_static:20m inactive=10m max_size=500m;
proxy_temp_path /tmp/engintron_temp;
## Virtual Host Configs ##
include /etc/nginx/conf.d/*.conf;
}