V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
phoenixcc
V2EX  ›  NGINX

利用 nginx 将开发环境的网页跳转打到本地服务上。

  •  
  •   phoenixcc · 5 天前 · 1102 次点击

    折腾了一天,怎么都不生效,后来我尝试将 www.baidu.com 跳转到 bing.com. 配置如下

    
    #user  nobody;
    worker_processes  1;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #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       80;
            server_name  www.baidu.com;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
            #    root   html;
            #    index  index.html index.htm;
    	    proxy_pass   https://www.bing.com/;
            }
    
            #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;
        #    }
        #}
        include servers/*;
    }
    

    我通过 curl 访问,看起来是可以的,但是为什么在浏览器上输入,却不行呢?

    sss@sssMacBook-Air ~ % curl www.baidu.com
    <html><head><title>Object moved</title></head><body>
    <h2>Object moved to <a href="https://cn.bing.com/">here</a>.</h2>
    </body></html>
    

    麻烦大佬们指点下啊。

    第 1 条附言  ·  4 天前
    经过尝试,我本地配置 host ,然后开启 nginx ,通过 chrome 隐私模式,访问 http://www.baidu.com 可以跳转,不过没办法跳转 https.
    因为 host 配置不支持 https. 看来这个方案不能解决将网页上的部分 https 的请求打到本地服务。
    有朋友推荐了 SwitchyOmega chrome 插件。我试一下看看
    8 条回复    2024-06-14 16:39:31 +08:00
    dallaslu
        1
    dallaslu  
       5 天前
    脱水版:

    ```nginx
    worker_processes 1;

    events {
    worker_connections 1024;
    }


    http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on; keepalive_timeout 65;

    server {
    listen 80;
    server_name www.baidu.com;

    location / {
    proxy_pass https://www.bing.com/;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    root html;
    }
    }
    include servers/*;
    }
    ```
    deplives
        2
    deplives  
       5 天前
    你首先需要先劫持 DNS 让浏览器解析百度的域名到你的服务器
    phoenixcc
        3
    phoenixcc  
    OP
       5 天前
    @deplives 这个我是本地直接改的 host ,将 www.baidu.com 映射到 127.0.0.1
    vituralfuture
        4
    vituralfuture  
       5 天前 via Android
    原因很简单啊,请求没发到 nginx ,自然没法帮你转发,看看 access.log 就能知道
    zcf0508
        5
    zcf0508  
       5 天前
    浏览器也会有自己的 dns 缓存,可以在 chrome://net-internals/#dns 页面清理,然后在页面上多刷新几次试试。

    可以检查一下页面的响应是不是来自 127.0.0.1 ,如果不是的话,就像楼上说的,请求没到 nginx 里。

    如果是前端开发有这个需求的话,可以看下我写的


    https://github.com/zcf0508/unplugin-https-reverse-proxy

    https://juejin.cn/post/7338239261193404466
    Xinu
        6
    Xinu  
       5 天前
    phoenixcc
        7
    phoenixcc  
    OP
       4 天前
    @vituralfuture 嗯嗯,配置了之后,发现确实没访问到,通过 curl 访问百度倒是可以跳转 bing 。后面发现使用隐私模式可以正常跳转了。
    shangguanshaofu
        8
    shangguanshaofu  
       4 天前
    不用那么麻烦,直接 proxy_pass http://$remote_addr:8888;
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2982 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 14:07 · PVG 22:07 · LAX 07:07 · JFK 10:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.