目录

使用Hosts文件解析域名

虚拟主机域名配置

不同端口号

相同端口号


PC在从DNS服务器拿到ip地址之后,会发起TCP/IP请求。这里要提一下http协议和tcp协议了,http在tcp只上(并不是说tcp是低级协议,只是因为它更偏向于基础协议)。http协议被联网设备广泛的应用。因为tcp协议只能以二进制,数据流的形式来发送数据(可查看之前的博客:为何说UDP面向报文,而TCP面向字节流)。这些数据像水一样不停的流过去,而如果中途出现了卡顿等情况,那我们就可以对之前所传输的数据先行处理。http协议中最关键的地方在于,在什么情况下可以表明双方的数据传递结束,而tcp协议是没有这种“终止符”功能的。


使用Hosts文件解析域名

hosts文件位置:

C:\Windows\System32\drivers\etc

在修改host文件之前需要对其权限进行修改,或者使用火绒等其他工具直接修改都可以。可以看到咱们的Users当前是不具备权限的,点击编辑: 

nginx透明tcp nginx tcp 域名_linux

选中Users,赋予其权限后点击确定:

nginx透明tcp nginx tcp 域名_linux_02

系统会询问是否修改,因为这样会降低安全等级,咱们点确定。可以在改完里面的内容后再把权限还原回去。 接下来咱们添加点内容:

nginx透明tcp nginx tcp 域名_服务器_03

前面的ip地址是我们虚拟机的ip,后面的域名自己随便写一个即可。保存后记得把权限还原回去。然后我们就可以去Ping一下看看效果了(这里Ping的是上面所写的域名): 

nginx透明tcp nginx tcp 域名_网络_04

也可以直接去访问看看(这里的Tomcat是我自己改的,无视即可):

nginx透明tcp nginx tcp 域名_服务器_05


虚拟主机域名配置

咱们先创建几个站点,位置可以自定,只要你能记住就行。我这里就直接创建在Nginx的html目录下了:

nginx透明tcp nginx tcp 域名_nginx透明tcp_06

这里一共创建了两个目录,一个叫 billy 一个叫 Mrbanana (嗯,不要问为啥叫这个)。两个目录下面分别创建了一个 index.html ,里面都随便写了点东西进去。(这里的出现的 q 是我定义的 cd .. )看一下大概的目录结构:

[root@van html]# tree ../html/
../html/
├── 50x.html
├── billy
│   └── index.html
├── index.html
├── index.php
└── Mrbanana
    └── index.html

 然后咱们去修改一下配置文件,直接在Xftp里面干了(直接右键编辑): 

nginx透明tcp nginx tcp 域名_linux_07

不同端口号

找到server以进行修改:

nginx透明tcp nginx tcp 域名_nginx_08

对其下面的 location 进行修改,咱们先把 billy 放进来,如下:

server {
        listen       80;
        server_name  localhost;

        location / {
            root   /usr/local/nginx/html/billy;
            index  index.html index.htm index.php index.java;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

然后咱们将这段再复制粘贴一遍,把端口号和目录改改,把香蕉君也放进去,第二个如下:

server {
        listen       81;
        server_name  localhost;

        location / {
            root   /usr/local/nginx/html/Mrbanana;
            index  index.html index.htm index.php index.java;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

保存退出后我们reload一下:

systemctl reload nginx.service

接着咱们就可以去看看成果了,用IP和域名访问都可以:

nginx透明tcp nginx tcp 域名_nginx透明tcp_09

nginx透明tcp nginx tcp 域名_网络_10


相同端口号

这里咱们把之前的端口号全部改成80,然后修改一下他们的 server_name 以进行区分:

server {
        listen       80;
        server_name  billy.van.com;

        location / {
            root   /usr/local/nginx/html/billy;
            index  index.html index.htm index.php index.java;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }

    }
server {
        listen       80;
        server_name  mrbanana.van.com;

        location / {
            root   /usr/local/nginx/html/Mrbanana;
            index  index.html index.htm index.php index.java;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

保存退出后记得 reload 一下:

systemctl reload nginx.service