利用 nginx 将开发环境的网页跳转打到本地服务上。 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
phoenixcc
V2EX    NGINX

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

  •  
  •   phoenixcc 2024-06-13 12:06:30 +08:00 2631 次点击
    这是一个创建于 551 天前的主题,其中的信息可能已经有所发展或是发生改变。

    折腾了一天,怎么都不生效,后来我尝试将 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 条附言    2024-06-14 09:04:03 +08:00
    经过尝试,我本地配置 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  
       2024-06-13 12:17:50 +08:00
    脱水版:

    ```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  
       2024-06-13 12:29:33 +08:00
    你首先需要先劫持 DNS 让浏览器解析百度的域名到你的服务器
    phoenixcc
        3
    phoenixcc  
    OP
       2024-06-13 13:44:48 +08:00
    @deplives 这个我是本地直接改的 host ,将 www.baidu.com 映射到 127.0.0.1
    vituralfuture
        4
    vituralfuture  
       2024-06-13 13:45:02 +08:00 via Android
    原因很简单啊,请求没发到 nginx ,自然没法帮你转发,看看 access.log 就能知道
    zcf0508
        5
    zcf0508  
       2024-06-13 13:54:04 +08:00
    浏览器也会有自己的 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  
       2024-06-13 15:35:10 +08:00
    phoenixcc
        7
    phoenixcc  
    OP
       2024-06-14 09:00:47 +08:00
    @vituralfuture 嗯嗯,配置了之后,发现确实没访问到,通过 curl 访问百度倒是可以跳转 bing 。后面发现使用隐私模式可以正常跳转了。
    shangguanshaofu
        8
    shangguanshaofu  
       2024-06-14 16:39:31 +08:00
    不用那么麻烦,直接 proxy_pass http://$remote_addr:8888;
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     4573 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 23ms UTC 01:07 PVG 09:07 LAX 17:07 JFK 20:07
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86