Nginx反向代理實現ipv4網站可以通過ipv6訪問


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM