使用nginx反向代理将外部网站加载到子文件夹根本不起作用

6uxekuva  于 5个月前  发布在  Nginx
关注(0)|答案(1)|浏览(68)

我需要加载外部网站到nginx子文件夹,我试了很多,仍然不工作,这是我现在有:
ngixn.coonfig

worker_processes  2;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {}

http {

upstream keepalive-upstream {
  server google.com:443;
  keepalive 64;
}

server {

      listen    9091 ssl;
     
      ssl_certificate /etc/ssl/nginx-selfsigned.crt;
      ssl_certificate_key /etc/ssl/nginx-selfsigned.key;

      location /looker {
                       proxy_pass https://keepalive-upstream;
                       proxy_http_version 1.1;
                       proxy_ssl_server_name on;
                       proxy_buffering         off;
                       proxy_set_header Accept-Encoding "";
                       proxy_set_header        Host  google.com;

                       sub_filter "https://google.com/looker" "https://localhost:9091/looker/";
                       sub_filter_types *;
                       sub_filter_once off;
                      
                   }

    location / {
        root /www;
      }
    }

}

字符串
从这里开始,我的目标只是将Google加载到https://localhost:9091/looker子文件夹中
但是当我运行的时候,我看到了谷歌的错误页面,错误是:

404. That’s an error.

The requested URL /looker was not found on this server. That’s all we know.


任何人都可以看看上面的nginx配置,并告诉我什么修复可以使它工作?
先谢了。

uujelgoq

uujelgoq1#

更新2023-12-23

#
# Nginx reverse proxy to Google Search (with images and webcache))
#
# Credit:
# * Nginx rewrite append a parameter at the end of an URL
#   https://serverfault.com/a/311660/387898
# * https://github.com/tracycool/Reverse-Proxy-for-Google
# * https://github.com/caiguanhao/nginx-bypass-gfw/blob/master/google.conf
#
# References:
# * Google Custom Search - CSE parameters list
#   https://developers.google.com/custom-search/json-api/v1/reference/cse/list
#
#
# Aaron LI
# 2017-05-23
#

server {
    listen            9091 ssl http2;
    server_name  g.example.com;

    # SSL/TLS Certificate kindly provided by Let's Encrypt
    ssl_certificate      /etc/ssl/nginx-selfsigned.crt;;
    ssl_certificate_key  /etc/ssl/nginx-selfsigned.key;

    # Enable caching
    #proxy_cache  CACHE;

    # Tune buffer
    proxy_buffer_size        64k;
    proxy_buffers            4 128k;
    proxy_busy_buffers_size  128k;

    # Replace cookie domain
    proxy_cookie_domain  google.com  $host;

    # Hide some upstream headers to avoid duplicates/overrideing
    proxy_hide_header  Strict-Transport-Security;
    proxy_hide_header  Content-Security-Policy;
    proxy_hide_header  X-Frame-Options;
    proxy_hide_header  X-XSS-Protection;
    proxy_hide_header  X-Content-Type-Options;
    proxy_hide_header  Referrer-Policy;

    # Substitute links in contents
    # NOTE: Require to set Accept-Encoding="" header in order to request
    #       *uncompressed* data from upstream, otherwise won't work!
    sub_filter_types  text/css text/javascript application/json;
    sub_filter_once   off;
    sub_filter  //www.google.com/                  //$host/looker/;
    sub_filter  //apis.google.com/                 //$host/looker/__gapis/;
    sub_filter  //ajax.googleapis.com/             //$host/looker/__gajax/;
    sub_filter  //fonts.googleapis.com/            //$host/looker/__gfonts/;
    sub_filter  //www.gstatic.com/                 //$host/looker/__gstatic/www/;
    sub_filter  //ssl.gstatic.com/                 //$host/looker/__gstatic/ssl/;
    sub_filter  //encrypted-tbn0.gstatic.com/      //$host/looker/__gstatic/enc-tbn0/;
                # Google Images
    sub_filter  //webcache.googleusercontent.com/  //$host/looker/__gwebcache/;

    # WARNING:
    # The "proxy_set_header" directives are inherited from the previous
    # level *if and only if* there are *no* such directives defined on
    # the current level!

    location / {
      root /www;
    }

    #
    # Reverse proxy to Google search and its friends :-)
    #
    location /looker/ {
        proxy_pass  https://www.google.com/;

        # These header need set explicitly, otherwise the browser will
        # be redirected to Google's URL without proxy...
        proxy_set_header  Host     www.google.com;
        proxy_set_header  Referer  https://www.google.com;
        # Set other necessary headers
        # NOTE: Set Accept-Encoding="" to request *uncompressed* data
        #       from upstream, otherwise "sub_filter" doesn't work!
        # Credit: https://stackoverflow.com/a/36274259
        proxy_set_header  User-Agent         $http_user_agent;
        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;
        proxy_set_header  Cookie             "";
        proxy_set_header  Accept-Language    "en-US";
        proxy_set_header  Accept-Encoding    "";

        # Append "&gfe_rd=cr&gws_rd=cr" to disable country redirection.
        # Append "&hl=en" to set interface language to English.
        #
        # "rewrite" matches against URL's *path* part only, which means
        # "$1" will *not* contain the query string.  And Nginx appends
        # original query string to the rewrite replacement by default.
        #
        # Credit: https://serverfault.com/a/311660/387898
        rewrite  ^/looker(.*)$  $1?gfe_rd=cr&gws_rd=cr&hl=en  break;
    }

    location ^~ /looker/__gwebcache/ {
        # ^~ will make location search stop here if matched.
        proxy_pass  https://webcache.googleusercontent.com/;
        # Note the trailing '/' above, which tells Nginx to strip the
        # matched URI.
        # Credit: https://serverfault.com/a/725433/387898

        proxy_set_header  Host     webcache.googleusercontent.com;
        proxy_set_header  Referer  https://webcache.googleusercontent.com;
        # NOTE: The upper level "proxy_set_header" directives are *not*
        #       inherited since there are such directives on this level!
        proxy_set_header  User-Agent         $http_user_agent;
        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;
        proxy_set_header  Cookie             "";
        proxy_set_header  Accept-Language    "en-US";
        proxy_set_header  Accept-Encoding    "";
    }
    location ^~ /looker/__gstatic/ssl/ {
        proxy_pass        https://ssl.gstatic.com/;
        proxy_set_header  Host     ssl.gstatic.com;
        proxy_set_header  Referer  https://ssl.gstatic.com;
        proxy_set_header  User-Agent         $http_user_agent;
        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;
        proxy_set_header  Cookie             "";
        proxy_set_header  Accept-Language    "en-US";
        proxy_set_header  Accept-Encoding    "";
    }
    location ^~ /looker/__gstatic/www/ {
        proxy_pass        https://www.gstatic.com/;
        proxy_set_header  Host     ssl.gstatic.com;
        proxy_set_header  Referer  https://ssl.gstatic.com;
        proxy_set_header  User-Agent         $http_user_agent;
        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;
        proxy_set_header  Cookie             "";
        proxy_set_header  Accept-Language    "en-US";
        proxy_set_header  Accept-Encoding    "";
    }
    location ^~ /looker/__gstatic/enc-tbn0/ {
        proxy_pass        https://encrypted-tbn0.gstatic.com/;
        proxy_set_header  Host     encrypted-tbn0.gstatic.com;
        proxy_set_header  Referer  https://encrypted-tbn0.gstatic.com;
        proxy_set_header  User-Agent         $http_user_agent;
        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;
        proxy_set_header  Cookie             "";
        proxy_set_header  Accept-Language    "en-US";
        proxy_set_header  Accept-Encoding    "";
    }
    location ^~ /looker/__gapis/ {
        proxy_pass        https://apis.google.com/;
        proxy_set_header  Host     apis.google.com;
        proxy_set_header  Referer  https://apis.google.com;
        proxy_set_header  User-Agent         $http_user_agent;
        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;
        proxy_set_header  Cookie             "";
        proxy_set_header  Accept-Language    "en-US";
        proxy_set_header  Accept-Encoding    "";
    }
    location ^~ /looker/__gfonts/ {
        proxy_pass        https://fonts.googleapis.com/;
        proxy_set_header  Host     fonts.googleapis.com;
        proxy_set_header  Referer  https://fonts.googleapis.com;
        proxy_set_header  User-Agent         $http_user_agent;
        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;
        proxy_set_header  Cookie             "";
        proxy_set_header  Accept-Language    "en-US";
        proxy_set_header  Accept-Encoding    "";
    }
    location ^~ /looker/__gajax/ {
        proxy_pass        https://ajax.googleapis.com/;
        proxy_set_header  Host     ajax.googleapis.com;
        proxy_set_header  Referer  https://ajax.googleapis.com;
        proxy_set_header  User-Agent         $http_user_agent;
        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;
        proxy_set_header  Cookie             "";
        proxy_set_header  Accept-Language    "en-US";
        proxy_set_header  Accept-Encoding    "";
    }

    # Forbid spider
    if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot") {
        return  403;
    }

    location /looker/robots.txt {
        default_type  text/plain;
        return  200  "User-agent: *\nDisallow: /\n";
    }
}

字符串

相关问题