Nginx之静态文件服务器的搭建
创始人
2025-01-16 22:04:40
0

1.概述

        静态文件服务器是指提供HTML文件访问或客户端 可直接从中下载文件的Web服务器。对于图片、 JavaScript或CSS文件等渲染页面外观的、不会动态改 变内容的文件,大多数网站会单独提供以静态文件服 务器的方式对其进行访问,实现动静分离的架构。

        HTML是一种标记语言,提供HTML文件读取是Web服 务器最基本的功能,Web服务器的配置样例如下:

server {     listen 8080;     root /opt/nginx-web/www; #存放静态文件的文件目录     location / {         index index.html;     }     location /js {         alias /opt/nginx-web/static/js/; #存放JavaScript文件的文件目录         index index.html;     } }

        在以上配置中,每个server指令域等同于一个虚 拟服务器,每个location指令域等同于一个虚拟目录

2.实验

        按照上述配置后,我们在/opt/nginx-web/www下放置一个index.html文件

#1.查看配置文件 [root@ansible01 nginx]# cat nginx.conf |grep -v "#"|grep -v "^$" user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; include /usr/share/nginx/modules/*.conf; events {     worker_connections 1024; } http {     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                       '$status $body_bytes_sent "$http_referer" '                       '"$http_user_agent" "$http_x_forwarded_for"';     access_log  /var/log/nginx/access.log  main;     sendfile            on;     tcp_nopush          on;     tcp_nodelay         on;     keepalive_timeout   65;     types_hash_max_size 4096;     include             /etc/nginx/mime.types;     default_type        application/octet-stream;     include /etc/nginx/conf.d/*.conf;     server {         listen       8080;         listen       [::]:8080;         server_name  11.0.1.18;         root         /opt/nginx-web/www;         include /etc/nginx/default.d/*.conf;         location / {             index  index.html index.htm;         }         error_page 404 /404.html;         location = /404.html {         }         error_page 500 502 503 504 /50x.html;         location = /50x.html {         }     } } #2.查看index.html文件 [root@ansible01 nginx]# cat /opt/nginx-web/www/index.html  hello world #3.重载nginx配置文件 [root@ansible01 nginx]# nginx -s reload #4.关闭防火墙 [root@ansible01 nginx]# systemctl stop firewalld #5.关闭selinux [root@ansible01 nginx]# setenforce 0 [root@ansible01 nginx]# getenforce  Permissive 

直接在windows用浏览器访问:11.0.1.18:8080

        2.1 基于域名的虚拟主机

                2.1.1 nginx配置

[root@ansible01 nginx]# cat nginx.conf |grep -v "#"|grep -v "^$" user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; include /usr/share/nginx/modules/*.conf; events {     worker_connections 1024; } http {     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                       '$status $body_bytes_sent "$http_referer" '                       '"$http_user_agent" "$http_x_forwarded_for"';     access_log  /var/log/nginx/access.log  main;     sendfile            on;     tcp_nopush          on;     tcp_nodelay         on;     keepalive_timeout   65;     types_hash_max_size 4096;     include             /etc/nginx/mime.types;     default_type        application/octet-stream;     include /etc/nginx/conf.d/*.conf;     server {         listen       80;         listen       [::]:80;         server_name  www.a.com;         root         /opt/nginx-web/www/a/;         include /etc/nginx/default.d/*.conf;         location / {             index  index.html index.htm;         }         error_page 404 /404.html;         location = /404.html {         }         error_page 500 502 503 504 /50x.html;         location = /50x.html {         }     }     server {         listen       80;         listen       [::]:80;         server_name  www.b.com;         root         /opt/nginx-web/www/b/;         include /etc/nginx/default.d/*.conf;         location / {             index  index.html index.htm;         }         error_page 404 /404.html;         location = /404.html {         }         error_page 500 502 503 504 /50x.html;         location = /50x.html {         }     } } 

        2.1.2 准备静态文件 

[root@ansible01 nginx]# cat /opt/nginx-web/www/a/index.html  hello,this is www.a.com [root@ansible01 nginx]# cat /opt/nginx-web/www/b/index.html  hello,this is www.b.com 

        2.1.3 重启服务,增加ip域名映射

[root@ansible01 nginx]# nginx -s reload [root@ansible01 nginx]# cat /etc/hosts 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6 11.0.1.18 www.a.com 11.0.1.18 www.b.com 

        2.1.4 测试 

[root@ansible01 nginx]# curl www.a.com hello,this is www.a.com [root@ansible01 nginx]# curl www.b.com hello,this is www.b.com 

        2.2 基于端口的虚拟主机

        2.2.1 nginx配置

[root@ansible01 nginx]# cat nginx.conf |grep -v "#"|grep -v "^$" user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; include /usr/share/nginx/modules/*.conf; events {     worker_connections 1024; } http {     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                       '$status $body_bytes_sent "$http_referer" '                       '"$http_user_agent" "$http_x_forwarded_for"';     access_log  /var/log/nginx/access.log  main;     sendfile            on;     tcp_nopush          on;     tcp_nodelay         on;     keepalive_timeout   65;     types_hash_max_size 4096;     include             /etc/nginx/mime.types;     default_type        application/octet-stream;     include /etc/nginx/conf.d/*.conf;     server {         listen       80;         listen       [::]:80;         server_name  www.test.com;         root         /opt/nginx-web/www/a/;         include /etc/nginx/default.d/*.conf;         location / {             index  index.html index.htm;         }         error_page 404 /404.html;         location = /404.html {         }         error_page 500 502 503 504 /50x.html;         location = /50x.html {         }     }     server {         listen       81;         listen       [::]:81;         server_name  www.test.com;         root         /opt/nginx-web/www/b/;         include /etc/nginx/default.d/*.conf;         location / {             index  index.html index.htm;         }         error_page 404 /404.html;         location = /404.html {         }         error_page 500 502 503 504 /50x.html;         location = /50x.html {         }     } } 

        2.2.2 重启服务,增加IP域名映射

[root@ansible01 nginx]# nginx -s reload [root@ansible01 nginx]# cat /etc/hosts 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6 11.0.1.18 www.a.com 11.0.1.18 www.b.com 11.0.1.18 www.test.com 

        2.2.3 测试

[root@ansible01 nginx]# curl www.test.com:80 hello,this is www.a.com [root@ansible01 nginx]# curl www.test.com:81 hello,this is www.b.com 

        2.3 基于IP的虚拟主机

        2.3.1 nginx配置

[root@ansible01 nginx]# cat nginx.conf |grep -v "#"|grep -v "^$" user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; include /usr/share/nginx/modules/*.conf; events {     worker_connections 1024; } http {     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                       '$status $body_bytes_sent "$http_referer" '                       '"$http_user_agent" "$http_x_forwarded_for"';     access_log  /var/log/nginx/access.log  main;     sendfile            on;     tcp_nopush          on;     tcp_nodelay         on;     keepalive_timeout   65;     types_hash_max_size 4096;     include             /etc/nginx/mime.types;     default_type        application/octet-stream;     include /etc/nginx/conf.d/*.conf;     server {         listen       11.0.1.18:80;         server_name  www.test.com;         root         /opt/nginx-web/www/a/;         include /etc/nginx/default.d/*.conf;         location / {             index  index.html index.htm;         }         error_page 404 /404.html;         location = /404.html {         }         error_page 500 502 503 504 /50x.html;         location = /50x.html {         }     }     server {         listen       11.0.1.19:80;         server_name  www.test.com;         root         /opt/nginx-web/www/b/;         include /etc/nginx/default.d/*.conf;         location / {             index  index.html index.htm;         }         error_page 404 /404.html;         location = /404.html {         }         error_page 500 502 503 504 /50x.html;         location = /50x.html {         }     } } 

        2.3.2 重启服务

[root@ansible01 nginx]# nginx -s reload

        2.3.3 测试

[root@ansible01 nginx]# curl 11.0.1.18 hello,this is www.a.com [root@ansible01 nginx]# curl 11.0.1.19 hello,this is www.b.com 

相关内容

热门资讯

一分钟内幕!科乐吉林麻将系统发... 一分钟内幕!科乐吉林麻将系统发牌规律,福建大玩家确实真的是有挂,技巧教程(有挂ai代打);所有人都在...
一分钟揭秘!微扑克辅助软件(透... 一分钟揭秘!微扑克辅助软件(透视辅助)确实是有挂(2024已更新)(哔哩哔哩);1、用户打开应用后不...
五分钟发现!广东雀神麻雀怎么赢... 五分钟发现!广东雀神麻雀怎么赢,朋朋棋牌都是是真的有挂,高科技教程(有挂方法)1、广东雀神麻雀怎么赢...
每日必看!人皇大厅吗(透明挂)... 每日必看!人皇大厅吗(透明挂)好像存在有挂(2026已更新)(哔哩哔哩);人皇大厅吗辅助器中分为三种...
重大科普!新华棋牌有挂吗(透视... 重大科普!新华棋牌有挂吗(透视)一直是有挂(2021已更新)(哔哩哔哩)1、完成新华棋牌有挂吗的残局...
二分钟内幕!微信小程序途游辅助... 二分钟内幕!微信小程序途游辅助器,掌中乐游戏中心其实存在有挂,微扑克教程(有挂规律)二分钟内幕!微信...
科技揭秘!jj斗地主系统控牌吗... 科技揭秘!jj斗地主系统控牌吗(透视)本来真的是有挂(2025已更新)(哔哩哔哩)1、科技揭秘!jj...
1分钟普及!哈灵麻将攻略小,微... 1分钟普及!哈灵麻将攻略小,微信小程序十三张好像存在有挂,规律教程(有挂技巧)哈灵麻将攻略小是一种具...
9分钟教程!科乐麻将有挂吗,传... 9分钟教程!科乐麻将有挂吗,传送屋高防版辅助(总是存在有挂)1、完成传送屋高防版辅助透视辅助安装,帮...
每日必看教程!兴动游戏辅助器下... 每日必看教程!兴动游戏辅助器下载(辅助)真是真的有挂(2025已更新)(哔哩哔哩)1、打开软件启动之...