Nginx反向代理实现ipv4网站可以通过ipv6访问

时间:2024-03-03 18:35:06

1、修改hosts文件,添加域名和本机Ip绑定,改完后重启网络服务

sudo vim /etc/hosts

 1 127.0.0.1    localhost www.web.com
 2 127.0.1.1    ChenXin
 3 
 4 # The following lines are desirable for IPv6 capable hosts
 5 ::1     ip6-localhost ip6-loopback www.web.com
 6 fe00::0 ip6-localnet
 7 ff00::0 ip6-mcastprefix
 8 ff02::1 ip6-allnodes
 9 ff02::2 ip6-allrouter
10 0.0.0.0 account.jetbrains.com
/etc/hosts

sudo /etc/init.d/networking restart

2、搭建webapp,这里使用uWSGI的接口搭建的简单的

1 def application(environ, start_response):
2     status = \'200 OK\'
3     output = \'Hello World!wocaocao\'
4  
5     response_headers = [(\'Content-type\', \'text/plain\'),
6                         (\'Content-Length\', str(len(output)))]
7     start_response(status, response_headers)
8  
9     return [output]
test.py

3、配置HTTP服务器,本次使用uWSGI,并打开服务

1 [uwsgi]
2 http=127.0.0.1:3031
3 wsgi-file=test.py
4 master=true
5 processes=1
6 threads=2
7 stats=127.0.0.1:9191
uwsgi.ini

sudo uwsgi uwsgi.ini

4、配置Nginx反向代理,并重载nginx配置,重启nginx服务

vim /etc/nginx/conf.d/www.web1.com.conf

 1 upstream test.com {
 2   server 127.0.0.1:3031;
 3 }
 4 
 5 
 6 server {
 7     listen [::]:80;
 8     server_name www.web1.com;
 9     access_log /var/log/nginx/www.web1.com_access.log;
10     error_log /var/log/nginx/www.web1.com_error.log;
11     location / {
12         proxy_set_header Host $host:$server_port;
13         proxy_set_header X-Real-IP $remote_addr;
14         proxy_set_header REMOTE-HOST $remote_addr;
15         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
16             proxy_pass http://test.com;
17     }
18 
19 }
www.web1.com.conf

sudo nginx -s reload

sudo service nginx restart

5、验证是否正常的命令

netstat -tulpn #查看监听端口

sudo nginx -t #查看nginx配置文件是否正确

ps -ef | grep nginx #查看nginx进程

 

参考文章:https://www.cnblogs.com/Erick-L/p/7066455.html