Nginx之静态文件服务器的搭建

打印 上一主题 下一主题

主题 556|帖子 556|积分 1668

1.概述

        静态文件服务器是指提供HTML文件访问或客户端 可直接从中下载文件的Web服务器。对于图片、 JavaScript或CSS文件等渲染页面外貌的、不会动态改 变内容的文件,大多数网站会单独提供以静态文件服 务器的方式对其进行访问,实现动静分离的架构。
        HTML是一种标记语言,提供HTML文件读取是Web服 务器最基本的功能,Web服务器的配置样例如下:
  1. server {
  2.     listen 8080;
  3.     root /opt/nginx-web/www; #存放静态文件的文件目录
  4.     location / {
  5.         index index.html;
  6.     }
  7.     location /js {
  8.         alias /opt/nginx-web/static/js/; #存放JavaScript文件的文件目录
  9.         index index.html;
  10.     }
  11. }
复制代码
        在以上配置中,每个server指令域等同于一个虚 拟服务器,每个location指令域等同于一个虚拟目次
2.实验

        按照上述配置后,我们在/opt/nginx-web/www下放置一个index.html文件
  1. #1.查看配置文件
  2. [root@ansible01 nginx]# cat nginx.conf |grep -v "#"|grep -v "^$"
  3. user nginx;
  4. worker_processes auto;
  5. error_log /var/log/nginx/error.log;
  6. pid /run/nginx.pid;
  7. include /usr/share/nginx/modules/*.conf;
  8. events {
  9.     worker_connections 1024;
  10. }
  11. http {
  12.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  13.                       '$status $body_bytes_sent "$http_referer" '
  14.                       '"$http_user_agent" "$http_x_forwarded_for"';
  15.     access_log  /var/log/nginx/access.log  main;
  16.     sendfile            on;
  17.     tcp_nopush          on;
  18.     tcp_nodelay         on;
  19.     keepalive_timeout   65;
  20.     types_hash_max_size 4096;
  21.     include             /etc/nginx/mime.types;
  22.     default_type        application/octet-stream;
  23.     include /etc/nginx/conf.d/*.conf;
  24.     server {
  25.         listen       8080;
  26.         listen       [::]:8080;
  27.         server_name  11.0.1.18;
  28.         root         /opt/nginx-web/www;
  29.         include /etc/nginx/default.d/*.conf;
  30.         location / {
  31.             index  index.html index.htm;
  32.         }
  33.         error_page 404 /404.html;
  34.         location = /404.html {
  35.         }
  36.         error_page 500 502 503 504 /50x.html;
  37.         location = /50x.html {
  38.         }
  39.     }
  40. }
  41. #2.查看index.html文件
  42. [root@ansible01 nginx]# cat /opt/nginx-web/www/index.html
  43. hello world
  44. #3.重载nginx配置文件
  45. [root@ansible01 nginx]# nginx -s reload
  46. #4.关闭防火墙
  47. [root@ansible01 nginx]# systemctl stop firewalld
  48. #5.关闭selinux
  49. [root@ansible01 nginx]# setenforce 0
  50. [root@ansible01 nginx]# getenforce
  51. Permissive
复制代码
直接在windows用浏览器访问:11.0.1.18:8080

        2.1 基于域名的虚拟主机

                2.1.1 nginx配置

  1. [root@ansible01 nginx]# cat nginx.conf |grep -v "#"|grep -v "^$"
  2. user nginx;
  3. worker_processes auto;
  4. error_log /var/log/nginx/error.log;
  5. pid /run/nginx.pid;
  6. include /usr/share/nginx/modules/*.conf;
  7. events {
  8.     worker_connections 1024;
  9. }
  10. http {
  11.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  12.                       '$status $body_bytes_sent "$http_referer" '
  13.                       '"$http_user_agent" "$http_x_forwarded_for"';
  14.     access_log  /var/log/nginx/access.log  main;
  15.     sendfile            on;
  16.     tcp_nopush          on;
  17.     tcp_nodelay         on;
  18.     keepalive_timeout   65;
  19.     types_hash_max_size 4096;
  20.     include             /etc/nginx/mime.types;
  21.     default_type        application/octet-stream;
  22.     include /etc/nginx/conf.d/*.conf;
  23.     server {
  24.         listen       80;
  25.         listen       [::]:80;
  26.         server_name  www.a.com;
  27.         root         /opt/nginx-web/www/a/;
  28.         include /etc/nginx/default.d/*.conf;
  29.         location / {
  30.             index  index.html index.htm;
  31.         }
  32.         error_page 404 /404.html;
  33.         location = /404.html {
  34.         }
  35.         error_page 500 502 503 504 /50x.html;
  36.         location = /50x.html {
  37.         }
  38.     }
  39.     server {
  40.         listen       80;
  41.         listen       [::]:80;
  42.         server_name  www.b.com;
  43.         root         /opt/nginx-web/www/b/;
  44.         include /etc/nginx/default.d/*.conf;
  45.         location / {
  46.             index  index.html index.htm;
  47.         }
  48.         error_page 404 /404.html;
  49.         location = /404.html {
  50.         }
  51.         error_page 500 502 503 504 /50x.html;
  52.         location = /50x.html {
  53.         }
  54.     }
  55. }
复制代码
        2.1.2 准备静态文件 

  1. [root@ansible01 nginx]# cat /opt/nginx-web/www/a/index.html
  2. hello,this is www.a.com
  3. [root@ansible01 nginx]# cat /opt/nginx-web/www/b/index.html
  4. hello,this is www.b.com
复制代码
        2.1.3 重启服务,增长ip域名映射

  1. [root@ansible01 nginx]# nginx -s reload
  2. [root@ansible01 nginx]# cat /etc/hosts
  3. 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
  4. ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
  5. 11.0.1.18 www.a.com
  6. 11.0.1.18 www.b.com
复制代码
        2.1.4 测试 

  1. [root@ansible01 nginx]# curl www.a.com
  2. hello,this is www.a.com
  3. [root@ansible01 nginx]# curl www.b.com
  4. hello,this is www.b.com
复制代码
        2.2 基于端口的虚拟主机

        2.2.1 nginx配置

  1. [root@ansible01 nginx]# cat nginx.conf |grep -v "#"|grep -v "^$"
  2. user nginx;
  3. worker_processes auto;
  4. error_log /var/log/nginx/error.log;
  5. pid /run/nginx.pid;
  6. include /usr/share/nginx/modules/*.conf;
  7. events {
  8.     worker_connections 1024;
  9. }
  10. http {
  11.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  12.                       '$status $body_bytes_sent "$http_referer" '
  13.                       '"$http_user_agent" "$http_x_forwarded_for"';
  14.     access_log  /var/log/nginx/access.log  main;
  15.     sendfile            on;
  16.     tcp_nopush          on;
  17.     tcp_nodelay         on;
  18.     keepalive_timeout   65;
  19.     types_hash_max_size 4096;
  20.     include             /etc/nginx/mime.types;
  21.     default_type        application/octet-stream;
  22.     include /etc/nginx/conf.d/*.conf;
  23.     server {
  24.         listen       80;
  25.         listen       [::]:80;
  26.         server_name  www.test.com;
  27.         root         /opt/nginx-web/www/a/;
  28.         include /etc/nginx/default.d/*.conf;
  29.         location / {
  30.             index  index.html index.htm;
  31.         }
  32.         error_page 404 /404.html;
  33.         location = /404.html {
  34.         }
  35.         error_page 500 502 503 504 /50x.html;
  36.         location = /50x.html {
  37.         }
  38.     }
  39.     server {
  40.         listen       81;
  41.         listen       [::]:81;
  42.         server_name  www.test.com;
  43.         root         /opt/nginx-web/www/b/;
  44.         include /etc/nginx/default.d/*.conf;
  45.         location / {
  46.             index  index.html index.htm;
  47.         }
  48.         error_page 404 /404.html;
  49.         location = /404.html {
  50.         }
  51.         error_page 500 502 503 504 /50x.html;
  52.         location = /50x.html {
  53.         }
  54.     }
  55. }
复制代码
        2.2.2 重启服务,增长IP域名映射

  1. [root@ansible01 nginx]# nginx -s reload
  2. [root@ansible01 nginx]# cat /etc/hosts
  3. 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
  4. ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
  5. 11.0.1.18 www.a.com
  6. 11.0.1.18 www.b.com
  7. 11.0.1.18 www.test.com
复制代码
        2.2.3 测试

  1. [root@ansible01 nginx]# curl www.test.com:80
  2. hello,this is www.a.com
  3. [root@ansible01 nginx]# curl www.test.com:81
  4. hello,this is www.b.com
复制代码
        2.3 基于IP的虚拟主机

        2.3.1 nginx配置

  1. [root@ansible01 nginx]# cat nginx.conf |grep -v "#"|grep -v "^$"
  2. user nginx;
  3. worker_processes auto;
  4. error_log /var/log/nginx/error.log;
  5. pid /run/nginx.pid;
  6. include /usr/share/nginx/modules/*.conf;
  7. events {
  8.     worker_connections 1024;
  9. }
  10. http {
  11.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  12.                       '$status $body_bytes_sent "$http_referer" '
  13.                       '"$http_user_agent" "$http_x_forwarded_for"';
  14.     access_log  /var/log/nginx/access.log  main;
  15.     sendfile            on;
  16.     tcp_nopush          on;
  17.     tcp_nodelay         on;
  18.     keepalive_timeout   65;
  19.     types_hash_max_size 4096;
  20.     include             /etc/nginx/mime.types;
  21.     default_type        application/octet-stream;
  22.     include /etc/nginx/conf.d/*.conf;
  23.     server {
  24.         listen       11.0.1.18:80;
  25.         server_name  www.test.com;
  26.         root         /opt/nginx-web/www/a/;
  27.         include /etc/nginx/default.d/*.conf;
  28.         location / {
  29.             index  index.html index.htm;
  30.         }
  31.         error_page 404 /404.html;
  32.         location = /404.html {
  33.         }
  34.         error_page 500 502 503 504 /50x.html;
  35.         location = /50x.html {
  36.         }
  37.     }
  38.     server {
  39.         listen       11.0.1.19:80;
  40.         server_name  www.test.com;
  41.         root         /opt/nginx-web/www/b/;
  42.         include /etc/nginx/default.d/*.conf;
  43.         location / {
  44.             index  index.html index.htm;
  45.         }
  46.         error_page 404 /404.html;
  47.         location = /404.html {
  48.         }
  49.         error_page 500 502 503 504 /50x.html;
  50.         location = /50x.html {
  51.         }
  52.     }
  53. }
复制代码
        2.3.2 重启服务

  1. [root@ansible01 nginx]# nginx -s reload
复制代码
        2.3.3 测试

  1. [root@ansible01 nginx]# curl 11.0.1.18
  2. hello,this is www.a.com
  3. [root@ansible01 nginx]# curl 11.0.1.19
  4. hello,this is www.b.com
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

熊熊出没

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表