• Nginx实战


    虚拟主机

    虚拟主机指的就是⼀个独⽴的站点,具有独⽴的域名,有完整的www服务,例如⽹站、FTP、邮件等 。Nginx⽀持多虚拟主机,在⼀台机器上可以运⾏完全独⽴的多个站点。⼀些草根流量站⻓,常会搭建个⼈站点进⾏资源分享交流,并且可能有多个不同业务的站点,如果每台服务器只运⾏⼀个⽹站,那么将造成资源浪费,成本浪费。利⽤虚拟主机的功能,就不⽤为了运⾏⼀个⽹站⽽单独配置⼀个Nginx服务器,或是单独再运⾏⼀组Nginx进程。虚拟主机可以在⼀台服务器,同⼀个Nginx进程上运⾏多个⽹站。

    搭建网站

    搭建一个静态网站,修改nginx.conf,⾃上⽽下找到第⼀个server{}指令块, 修改如下
    1. server {
    2. listen 80;
    3. server_name localhost;
    4. #默认编码
    5. charset utf-8;
    6. # access_log logs/host.access.log main;
    7. location / {
    8. #定义虚拟主机的资源目录,无论win或linux都要写成正斜杠
    9. root C:/Users/zhang/Desktop/new;
    10. #定义首页文件的名字
    11. index index.html index.htm;
    12. }
    13. }

    执行命令 

    1. // 检查配置是否正确
    2. nginx -t
    3. // 启动
    4. nginx
    5. // 重新加载Nginx配置文件
    6. nginx -s reload

    准备资源

    访问localhost

    访问静态资源

     

     nginx⽀持gzip对资源压缩传输,经过gzip压缩后的⻚⾯⼤⼩可以为原本的30%甚⾄更⼩,⽤户浏览体验会快很多

    1. #user nobody;
    2. worker_processes 1;
    3. #error_log logs/error.log;
    4. #error_log logs/error.log notice;
    5. #error_log logs/error.log info;
    6. #pid logs/nginx.pid;
    7. events {
    8. worker_connections 1024;
    9. }
    10. http {
    11. include mime.types;
    12. default_type application/octet-stream;
    13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    14. # '$status $body_bytes_sent "$http_referer" '
    15. # '"$http_user_agent" "$http_x_forwarded_for"';
    16. #access_log logs/access.log main;
    17. sendfile on;
    18. #tcp_nopush on;
    19. #keepalive_timeout 0;
    20. keepalive_timeout 65;
    21. // 开启压缩
    22. gzip on;
    23. gzip_http_version 1.1;
    24. gzip_comp_level 4;
    25. gzip_types text/plain application/javascript
    26. application/x-javascript text/css application/xml
    27. text/javascript application/x-httpd-php image/jpeg image/gif
    28. image/png;
    29. server {
    30. listen 80;
    31. server_name localhost;
    32. #默认编码
    33. charset utf-8;
    34. # access_log logs/host.access.log main;
    35. location / {
    36. #定义虚拟主机的资源目录,无论win或linux都要写成正斜杠
    37. root C:/Users/zhang/Desktop/new;
    38. #定义首页文件的名字
    39. index index.html index.htm;
    40. }
    41. }
    42. }

    下载站点功能

    nginx使用的是模块ngx_http_autoindex_module来实现的,该模块处理以斜杠("/")结尾的请求,并生成目录列表。nginx编译的时候会自动加载该模块,但是该模块默认是关闭的,我们需要使用下来指令来完成对应的配置

    autoindex

    启用或禁用目录列表输出

    语法autoindex on|off;
    默认值autoindex off;
    位置http、server、location

    autoindex_exact_size

    对应HTLM格式,指定是否在目录列表展示文件的详细大小

    默认为on,显示出文件的确切大小,单位是bytes。 改为off后,显示出文件的大概大小,单位是kB或者MB或者GB

    语法autoindex_exact_size on|off;
    默认值autoindex_exact_size on;
    位置http、server、location

    autoindex_format

    设置目录列表的格式

    语法autoindex_format html|xml|json|jsonp;
    默认值autoindex_format html;
    位置http、server、location

    注意:该指令在1.7.9及以后版本中出现

    autoindex_localtime

    对应HTML格式,是否在目录列表上显示时间。

    默认为off,显示的文件时间为GMT时间。 改为on后,显示的文件时间为文件的服务器时间

    语法autoindex_localtime on | off;
    默认值autoindex_localtime off;
    位置http、server、location

    配置方式如下

    1. location /download{
    2. root /usr/local;
    3. autoindex on;
    4. autoindex_exact_size on;
    5. autoindex_format html;
    6. autoindex_localtime on;
    7. }

    修改nginx.conf支持游览目录

    1. #user nobody;
    2. worker_processes 1;
    3. #error_log logs/error.log;
    4. #error_log logs/error.log notice;
    5. #error_log logs/error.log info;
    6. #pid logs/nginx.pid;
    7. events {
    8. worker_connections 1024;
    9. }
    10. http {
    11. include mime.types;
    12. default_type application/octet-stream;
    13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    14. # '$status $body_bytes_sent "$http_referer" '
    15. # '"$http_user_agent" "$http_x_forwarded_for"';
    16. #access_log logs/access.log main;
    17. sendfile on;
    18. #tcp_nopush on;
    19. #keepalive_timeout 0;
    20. keepalive_timeout 65;
    21. # gzip on;
    22. #第一个虚拟主机
    23. server {
    24. #监听的端口和ip
    25. listen 80;
    26. #主机域名
    27. server_name www.test1.com;
    28. charset utf-8;
    29. access_log logs/host.access.log;
    30. #url匹配
    31. location / {
    32. #HTML文件存放的目录
    33. root C:/Users/zhang/Desktop/new/web2;
    34. #开启目录索引功能
    35. autoindex on;
    36. #关闭详细文件大小统计,让文件大小显示MB,GB单位,默认为b;
    37. autoindex_exact_size off;
    38. #开启以服务器本地时区显示文件修改日期!
    39. autoindex_localtime on;
    40. }
    41. }
    42. }

     访问

    基于IP多虚拟主机

    修改 nginx.conf ⽀持多虚拟主机
    1. #user nobody;
    2. worker_processes 1;
    3. #error_log logs/error.log;
    4. #error_log logs/error.log notice;
    5. #error_log logs/error.log info;
    6. #pid logs/nginx.pid;
    7. events {
    8. worker_connections 1024;
    9. }
    10. http {
    11. include mime.types;
    12. default_type application/octet-stream;
    13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    14. # '$status $body_bytes_sent "$http_referer" '
    15. # '"$http_user_agent" "$http_x_forwarded_for"';
    16. #access_log logs/access.log main;
    17. sendfile on;
    18. #tcp_nopush on;
    19. #keepalive_timeout 0;
    20. keepalive_timeout 65;
    21. gzip on;
    22. gzip_http_version 1.1;
    23. gzip_comp_level 4;
    24. gzip_types text/plain application/javascript
    25. application/x-javascript text/css application/xml
    26. text/javascript application/x-httpd-php image/jpeg image/gif
    27. image/png;
    28. #第一个虚拟主机
    29. server {
    30. #监听的端⼝和ip
    31. listen 127.0.0.1:80;
    32. #主机域名
    33. server_name 127.0.0.1;
    34. charset utf-8;
    35. access_log logs/host.access.log;
    36. #url匹配
    37. location / {
    38. #HTML文件存放的目录
    39. root C:/Users/zhang/Desktop/new/web1;
    40. #默认首页文件,从左往右寻找,index.html或是index.htm
    41. index index.html index.htm;
    42. }
    43. }
    44. #第二个虚拟主机
    45. server {
    46. listen 192.168.52.100:80;
    47. server_name 192.168.52.100;
    48. location / {
    49. index index.html index.htm;
    50. root C:/Users/zhang/Desktop/new/web2;
    51. }
    52. }
    53. }

    执行命令

    1. #重载nginx
    2. nginx -s reload

    访问站点一

    访问站点二

    基于域名多虚拟主机

     基于多IP的虚拟主机可能会造成IP地址不⾜的问题,如果没有特殊需求,更常⽤的是基于多域名的形式。只需要你单独配置DNS服务器,将主机名对应到正确的IP地址,修改Nginx配置,可以识别到不同的主机即可,这样就可以使得多个虚拟主机⽤同⼀个IP,解决了IP不足的隐患。

    打开C:\Windows\System32\drivers\etc,配置域名。 

    修改 nginx.conf ⽀持多域名的虚拟主机
    1. #user nobody;
    2. worker_processes 1;
    3. #error_log logs/error.log;
    4. #error_log logs/error.log notice;
    5. #error_log logs/error.log info;
    6. #pid logs/nginx.pid;
    7. events {
    8. worker_connections 1024;
    9. }
    10. http {
    11. include mime.types;
    12. default_type application/octet-stream;
    13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    14. # '$status $body_bytes_sent "$http_referer" '
    15. # '"$http_user_agent" "$http_x_forwarded_for"';
    16. #access_log logs/access.log main;
    17. sendfile on;
    18. #tcp_nopush on;
    19. #keepalive_timeout 0;
    20. keepalive_timeout 65;
    21. gzip on;
    22. gzip_http_version 1.1;
    23. gzip_comp_level 4;
    24. gzip_types text/plain application/javascript
    25. application/x-javascript text/css application/xml
    26. text/javascript application/x-httpd-php image/jpeg image/gif
    27. image/png;
    28. #第一个虚拟主机
    29. server {
    30. #监听的端?和ip
    31. listen 80;
    32. #主机域名
    33. server_name www.test1.com;
    34. charset utf-8;
    35. access_log logs/host.access.log;
    36. #url匹配
    37. location / {
    38. #HTML文件存放的目录
    39. root C:/Users/zhang/Desktop/new/web1;
    40. #默认首页文件,从左往右寻找,index.html或是index.htm
    41. index index.html index.htm;
    42. }
    43. }
    44. #第二个虚拟主机
    45. server {
    46. listen 80;
    47. server_name www.test2.com;
    48. location / {
    49. index index.html index.htm;
    50. root C:/Users/zhang/Desktop/new/web2;
    51. }
    52. }
    53. }

    执行命令

    1. #重载nginx
    2. nginx -s reload

    访问域名1

    访问域名2

    基于端口多虚拟机

    基于端⼝的配置在⽣产环境⽐较少⻅,⽤于特殊场景,例如公司内部测试平台⽹站,使⽤特殊端⼝的后台,OA 系统、⽹站后台, CRM 后台等。

    修改nginx.conf⽀持多域名的虚拟主机

    1. #user nobody;
    2. worker_processes 1;
    3. #error_log logs/error.log;
    4. #error_log logs/error.log notice;
    5. #error_log logs/error.log info;
    6. #pid logs/nginx.pid;
    7. events {
    8. worker_connections 1024;
    9. }
    10. http {
    11. include mime.types;
    12. default_type application/octet-stream;
    13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    14. # '$status $body_bytes_sent "$http_referer" '
    15. # '"$http_user_agent" "$http_x_forwarded_for"';
    16. #access_log logs/access.log main;
    17. sendfile on;
    18. #tcp_nopush on;
    19. #keepalive_timeout 0;
    20. keepalive_timeout 65;
    21. gzip on;
    22. gzip_http_version 1.1;
    23. gzip_comp_level 4;
    24. gzip_types text/plain application/javascript
    25. application/x-javascript text/css application/xml
    26. text/javascript application/x-httpd-php image/jpeg image/gif
    27. image/png;
    28. #第一个虚拟主机
    29. server {
    30. #监听的端口和ip
    31. listen 80;
    32. #主机域名
    33. server_name www.test1.com;
    34. charset utf-8;
    35. access_log logs/host.access.log;
    36. #url匹配
    37. location / {
    38. #HTML文件存放的目录
    39. root C:/Users/zhang/Desktop/new/web1;
    40. #默认首页文件,从左往右寻找,index.html或是index.htm
    41. index index.html index.htm;
    42. }
    43. }
    44. #第二个虚拟主机
    45. server {
    46. listen 81;
    47. server_name www.test1.com;
    48. location / {
    49. index index.html index.htm;
    50. root C:/Users/zhang/Desktop/new/web2;
    51. }
    52. }
    53. }

    执行命令

    1. #重载nginx
    2. nginx -s reload

    访问端口80

    访问端口81

    root和alias区别

    1. Nginx的配置文件server中指定两个location执行,分别为root和alias的指令

    1. location /test/{
    2. alias /www/test/;
    3. }

    按照上述配置,则访问/test/目录里面的文件时,nginx会去/www/test目录找文件

    1. location /test/{
    2. root /www/test;
    3. }

    按照上述配置,则访问/test/目录下的文件时,nginx会去/www/test/test/目录下找文件

    2.alias是一个目录别名的定义,root则是最上层目录的定义。

    3,另一个区别是alias后面必须要用"/"结束,否则会找不到文件,而root则对"/"可有可无

    4.不少人都有误区,认为root是指的/www/test目录下,但是应该是/www/test/test目录

  • 相关阅读:
    区块链技术与应用
    Codeforces Round #811 (Div. 3)
    SpringBoot+Vue实现前后端分离的教学管理平台
    买房怎样申请贷款
    C++-容器-string:string的大小和容量
    MySQL中的SHOW FULL PROCESSLIST命令
    HashMap安全嘛? 不安全该怎么办【几种解决方法】【详细】
    uniapp 微信小程ios端键盘弹起后导致页面无法滚动
    Folyd
    hadoop集群搭建
  • 原文地址:https://blog.csdn.net/qq_63431773/article/details/133241326