• Nginx - 虚拟主机与域名解析


    目录

    使用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当前是不具备权限的,点击编辑: 

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

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

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

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


    虚拟主机域名配置

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

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

    1. [root@van html]# tree ../html/
    2. ../html/
    3. ├── 50x.html
    4. ├── billy
    5. │   └── index.html
    6. ├── index.html
    7. ├── index.php
    8. └── Mrbanana
    9. └── index.html

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

    不同端口号

    找到server以进行修改:

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

    1. server {
    2. listen 80;
    3. server_name localhost;
    4. location / {
    5. root /usr/local/nginx/html/billy;
    6. index index.html index.htm index.php index.java;
    7. }
    8. error_page 500 502 503 504 /50x.html;
    9. location = /50x.html {
    10. root html;
    11. }
    12. location ~ \.php$ {
    13. root html;
    14. fastcgi_pass 127.0.0.1:9000;
    15. fastcgi_index index.php;
    16. fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
    17. include fastcgi_params;
    18. }
    19. }

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

    1. server {
    2. listen 81;
    3. server_name localhost;
    4. location / {
    5. root /usr/local/nginx/html/Mrbanana;
    6. index index.html index.htm index.php index.java;
    7. }
    8. error_page 500 502 503 504 /50x.html;
    9. location = /50x.html {
    10. root html;
    11. }
    12. location ~ \.php$ {
    13. root html;
    14. fastcgi_pass 127.0.0.1:9000;
    15. fastcgi_index index.php;
    16. fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
    17. include fastcgi_params;
    18. }
    19. }

    保存退出后我们reload一下:

    systemctl reload nginx.service 

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


    相同端口号

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

    1. server {
    2. listen 80;
    3. server_name billy.van.com;
    4. location / {
    5. root /usr/local/nginx/html/billy;
    6. index index.html index.htm index.php index.java;
    7. }
    8. error_page 500 502 503 504 /50x.html;
    9. location = /50x.html {
    10. root html;
    11. }
    12. location ~ \.php$ {
    13. root html;
    14. fastcgi_pass 127.0.0.1:9000;
    15. fastcgi_index index.php;
    16. fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
    17. include fastcgi_params;
    18. }
    19. }
    1. server {
    2. listen 80;
    3. server_name mrbanana.van.com;
    4. location / {
    5. root /usr/local/nginx/html/Mrbanana;
    6. index index.html index.htm index.php index.java;
    7. }
    8. error_page 500 502 503 504 /50x.html;
    9. location = /50x.html {
    10. root html;
    11. }
    12. location ~ \.php$ {
    13. root html;
    14. fastcgi_pass 127.0.0.1:9000;
    15. fastcgi_index index.php;
    16. fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
    17. include fastcgi_params;
    18. }
    19. }

    保存退出后记得 reload 一下:

    systemctl reload nginx.service

  • 相关阅读:
    vue2 mixins混入
    纯CSS实现轮播图
    haproxy+keepalived实战
    房屋信贷违约风险竞争(kaggle)系列4-基准模型
    跨境电商首选腾讯云轻量应用服务器Lighthouse!
    JavaScript学习之路---JavaScript操作BOM
    使用百度EasyDL实现立体库智能盘点
    图的结构模板及遍历
    黑马点评-短信登录业务
    vsomeip3 双机通信文件配置
  • 原文地址:https://blog.csdn.net/Trollz/article/details/126541977