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

13 天前
 phoenixcc

折腾了一天,怎么都不生效,后来我尝试将 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>

麻烦大佬们指点下啊。

1206 次点击
所在节点    NGINX
8 条回复
dallaslu
13 天前
脱水版:

```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
13 天前
你首先需要先劫持 DNS 让浏览器解析百度的域名到你的服务器
phoenixcc
13 天前
@deplives 这个我是本地直接改的 host ,将 www.baidu.com 映射到 127.0.0.1
vituralfuture
13 天前
原因很简单啊,请求没发到 nginx ,自然没法帮你转发,看看 access.log 就能知道
zcf0508
13 天前
浏览器也会有自己的 dns 缓存,可以在 chrome://net-internals/#dns 页面清理,然后在页面上多刷新几次试试。

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

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


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

https://juejin.cn/post/7338239261193404466
Xinu
13 天前
phoenixcc
12 天前
@vituralfuture 嗯嗯,配置了之后,发现确实没访问到,通过 curl 访问百度倒是可以跳转 bing 。后面发现使用隐私模式可以正常跳转了。
shangguanshaofu
12 天前
不用那么麻烦,直接 proxy_pass http://$remote_addr:8888;

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1049201

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX