其实很久之前就分享过,不过最近看到一个支持手机适应的反代站,不错,抄过来。

没什么复杂的,直接给出 nginx 配置,使用sub_filter替换,根本用不上以前说过的第三方模块subs_filter

server {
    listen 80;
    listen [::]:80;
    server_name hostloc.eu.org;
    #SSL Configuration
    listen 443 ssl;
    listen [::]:443 ssl;
    ssl_certificate /ssl/hostloc.eu.org.crt;
    ssl_certificate_key /ssl/hostloc.eu.org.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    if ($scheme = http) {
      return 301 https://$server_name$request_uri;
    }
    location ~* "^/responsive.css$"  {
      root /data/web/webapps/hostloc.eu.org;
    }
    location ~* "^/polish.js$" {
      root /data/web/webapps/hostloc.eu.org;
    }
    # proxy to another site
    location / {
      proxy_pass https://hostloc.com;
      proxy_set_header Host hostloc.com;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      #proxy_set_header REMOTE-HOST $remote_addr;
      proxy_set_header referer https://hostloc.com/$request_uri;
      proxy_set_header Accept-Encoding "";
      proxy_set_header User-Agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0";
      add_header Access-Control-Allow-Origin * always;
      add_header Access-Control-Allow-Headers *;
      add_header Access-Control-Allow-Methods "GET, POST, PUT, OPTIONS";
      sub_filter_types *;
      sub_filter 'www.hostloc.com' 'hostloc.eu.org';
      sub_filter 'hostloc.com' 'hostloc.eu.org';
      sub_filter '<head>' '<head><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">';
      sub_filter '</body>' '<link rel="stylesheet" type="text/css" href="https://hostloc.eu.org/responsive.css" /><script src="https://hostloc.eu.org/polish.js"></script></body>';
      sub_filter_once off;
    }
}

必备的 css 和 js 文件,放到/www/hostloc.eu.org/下就行

https://hostloc.ir/static2/css/responsive.css
https://hostloc.ir/static2/js/polish.js

以前写过的

nginx 这个轻量级、高性能的 web server 主要可以干两件事情:1、直接作为http server(需要Fastcgi配合);2、作为反代服务器(进一步可以实现均衡负载)。这里主要利用一下反功能来方便一下日常生活。选择Gravatar当例子是因为尼玛我发现了我这的移动把Gravatar整站都墙了,还有hostloc也是移动访问困难。#update2016.01.11:反代Google想偷懒可以使用我的一键包一、Nginx反代超详细教程:Nginx安装和基本操作1、可以使用一键包...