折腾了一天,怎么都不生效,后来我尝试将 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
dallaslu 144 天前
脱水版:
```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/*; } ``` |
2
deplives 144 天前
你首先需要先劫持 DNS 让浏览器解析百度的域名到你的服务器
|
3
phoenixcc OP @deplives 这个我是本地直接改的 host ,将 www.baidu.com 映射到 127.0.0.1
|
4
vituralfuture 144 天前 via Android
原因很简单啊,请求没发到 nginx ,自然没法帮你转发,看看 access.log 就能知道
|
5
zcf0508 144 天前
浏览器也会有自己的 dns 缓存,可以在 chrome://net-internals/#dns 页面清理,然后在页面上多刷新几次试试。
可以检查一下页面的响应是不是来自 127.0.0.1 ,如果不是的话,就像楼上说的,请求没到 nginx 里。 如果是前端开发有这个需求的话,可以看下我写的 https://github.com/zcf0508/unplugin-https-reverse-proxy https://juejin.cn/post/7338239261193404466 |
6
Xinu 144 天前
|
7
phoenixcc OP @vituralfuture 嗯嗯,配置了之后,发现确实没访问到,通过 curl 访问百度倒是可以跳转 bing 。后面发现使用隐私模式可以正常跳转了。
|
8
shangguanshaofu 143 天前
不用那么麻烦,直接 proxy_pass http://$remote_addr:8888;
|