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

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

sudo gedit /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服务

sudo gedit /etc/nginx/sites-enabled/default

 1 ##
 2 # You should look at the following URL's in order to grasp a solid understanding
 3 # of Nginx configuration files in order to fully unleash the power of Nginx.
 4 # http://wiki.nginx.org/Pitfalls
 5 # http://wiki.nginx.org/QuickStart
 6 # http://wiki.nginx.org/Configuration
 7 #
 8 # Generally, you will want to move this file somewhere, and start with a clean
 9 # file but keep this around for reference. Or just disable in sites-enabled.
10 #
11 # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
12 ##
13 
14 # Default server configuration
15 #
16 server {
17     listen 8088 default_server;
18     listen [::]:8066 ipv6only=on; 
19 
20     # SSL configuration
21     #
22     # listen 443 ssl default_server;
23     # listen [::]:443 ssl default_server;
24     #
25     # Note: You should disable gzip for SSL traffic.
26     # See: https://bugs.debian.org/773332
27     #
28     # Read up on ssl_ciphers to ensure a secure configuration.
29     # See: https://bugs.debian.org/765782
30     #
31     # Self signed certs generated by the ssl-cert package
32     # Don't use them in a production server!
33     #
34     # include snippets/snakeoil.conf;
35 
36     root /var/www/html;
37 
38     # Add index.php to the list if you are using PHP
39     index index.html index.htm index.nginx-debian.html;
40 
41     #server_name _;
42      server_name  www.web1.com;
43 
44     #location / {
45         # First attempt to serve request as file, then
46         # as directory, then fall back to displaying a 404.
47           #include uwsgi_params;
48               #uwsgi_pass 127.0.0.1:3031;
49     #}
50 
51     location / {
52         #index index.html;
53         proxy_set_header Host $host:$server_port;
54         proxy_set_header X-Real-IP $remote_addr;
55         proxy_set_header REMOTE-HOST $remote_addr;
56         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
57         proxy_pass   http://sa;
58     }
59 
60     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
61     #
62     #location ~ \.php$ {
63     #    include snippets/fastcgi-php.conf;
64     #
65     #    # With php7.0-cgi alone:
66     #    fastcgi_pass 127.0.0.1:9000;
67     #    # With php7.0-fpm:
68     #    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
69     #}
70 
71     # deny access to .htaccess files, if Apache's document root
72     # concurs with nginx's one
73     #
74     #location ~ /\.ht {
75     #    deny all;
76     #}
77 }
78 upstream sa{
79     server 127.0.0.1:3031;   #SA Server1
80 }
81 
82 # Virtual Host configuration for example.com
83 #
84 # You can move that to a different file under sites-available/ and symlink that
85 # to sites-enabled/ to enable it.
86 #
87 #server {
88 #    listen 80;
89 #    listen [::]:80;
90 #
91 #    server_name example.com;
92 #
93 #    root /var/www/example.com;
94 #    index index.html;
95 #
96 #    location / {
97 #        try_files $uri $uri/ =404;
98 #    }
99 #}
/etc/nginx/sites-enabled/default

sudo nginx -s reload

sudo service nginx restart

5、验证是否正常的命令

netstat -tulpn #查看监听端口

sudo nginx -t #查看nginx服务是否正常

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

猜你喜欢

转载自www.cnblogs.com/cx59244405/p/9272128.html