Skip to content

Instantly share code, notes, and snippets.

@weekwood
Last active December 14, 2015 19:39
Show Gist options
  • Save weekwood/5137721 to your computer and use it in GitHub Desktop.
Save weekwood/5137721 to your computer and use it in GitHub Desktop.
server {
listen 80;
server_name www.mysite.com;
root /path/to/main_site;
# ...
location / {
proxy_set_header X-Real-IP $remote_addr;
# ...
if ($http_user_agent ~* '(iPhone|iPod)') {
set $iphone_request '1';
}
if ($http_cookie ~ 'iphone_mode=full') {
set $iphone_request '';
}
if ($iphone_request = '1') {
rewrite ^.+ http://m.mysite.com$uri;
}
# serve cached pages ...
if (!-f $request_filename) {
proxy_pass http://app_server;
break;
}
}
}
upstream m_app_server {
server 0.0.0.0:3001;
}
server {
listen 80;
server_name m.mysite.com;
root /path/to/mobile_site;
# ...
location / {
proxy_set_header X-Real-IP $remote_addr;
# ...
if ($http_user_agent ~* '(iPhone|iPod)') {
set $iphone_request '1';
set $iphone_path_prefix '/iphone';
}
if ($uri ~ ^/iphone.*$) {
set $iphone_path_prefix '';
}
if ($uri ~ '(images|stylesheets|javascripts|\.css|\.js|\.ico|\.gif|\.jpg|\.png)') {
set $iphone_path_prefix '';
}
if ($iphone_request = '1') {
rewrite (.*) $iphone_path_prefix$1;
}
# serve cached pages ...
if (!-f $request_filename) {
proxy_pass http://m_app_server;
break;
}
}
}
function createCookie(name, value, days, domain, path) {
var expires = '';
if (days) {
var d = new Date();
d.setTime(d.getTime() + (days*24*60*60*1000));
expires = '; expires=' + d.toGMTString();
}
domain = domain ? '; domain=' + domain : '';
path = '; path=' + (path ? path : '/');
document.cookie = name + '=' + value + expires + path + domain;
}
function readCookie(name) {
var n = name + '=';
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var c = cookies[i].replace(/^\s+/, '');
if (c.indexOf(n) == 0) {
return c.substring(n.length);
}
}
return null;
}
function eraseCookie(name, domain, path) {
setCookie(name, '', -1, domain, path);
}
<a href="http://www.mysite.com" onclick="createCookie('iphone_mode', 'full', (0.25/24), 'mysite.com')">
View full site
</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment