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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1450923.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

C# WPF入门学习主线篇(二十六)—— 绑定路径和数据上下文

C# WPF入门学习主线篇(二十六)—— 绑定路径和数据上下文 在WPF(Windows Presentation Foundation)中,数据绑定是一个核心概念,它允许你将UI控件的属性与数据源属性进行绑定,从而实现数据和UI的…

产品人生(13):从“产品的RFM分析”看如何探索“职业方向”

我们在做产品分析时,经常会用到一种方法“产品的RFM分析”,它是一种客户细分和价值评估的常用方法,广泛应用于电子商务、零售和其他众多行业,它可以帮助企业和产品团队更好地理解用户行为,优化营销策略,提升…

全开源无加密跨境电子商城系统源码

跨境电子商城系统源码是一套完整的电子商务平台开发解决方案,它涵盖了前端页面、后端管理、数据库设计等多个方面。企业通过使用这套源码,可以快速搭建起自己的跨境电子商城,从而省去从零开始开发的繁琐过程。 什么是跨境电子商城系统源码&a…

基于Python+OpenCV的车牌识别停车场管理系统(PyQt界面)【含Python源码 MX_009期】

简介: 基于Python和OpenCV的车牌识别停车场管理系统是一种利用计算机视觉技术来自动识别停车场进出车辆的系统。该系统通过摄像头捕获车辆图像,并使用OpenCV库中的图像处理和模式识别技术来识别图像中的车牌号码。一旦车牌被成功识别,系统就会…

实战指南:理解 ThreadLocal 原理并用于Java 多线程上下文管理

目录 一、ThreadLocal基本知识回顾分析 (一)ThreadLocal原理 (二)既然ThreadLocalMap的key是弱引用,GC之后key是否为null? (三)ThreadLocal中的内存泄漏问题及JDK处理方法 &…

公司面试题总结(五)

25.谈一谈箭头函数与普通函数的区别,箭头函数主要解决什么问题? 箭头函数与普通函数的区别: ⚫ 语法简洁性: ◼ 箭头函数使用>符号定义,省略了 function 关键字,使得语法更为紧凑。 ◼ 对于单行函…

Adobe illustrator教程——超实用的三个进阶小技巧!

AI2024(64bit) Adobe illustrator 软件安装包下载地址: 百度网盘下载https://pan.baidu.com/s/1C10-2JVN1rxFF5VFRuV2Yw?pwdSIMS 01 进阶技巧1——曲率工具 基于之前的入门教程,大家肯定会快速想到“画笔工具”,但是画出来的曲线往往不够平…

12.实战私有数据微调ChatGLM3

实战私有数据微调ChatGLM3 实战私有数据微调ChatGLM3实战构造私有的微调数据集基于 ChatGPT 设计生成训练数据的 Prompt使用 LangChain GPT-3.5-Turbo 生成训练数据样例训练数据解析、数据增强和持久化存储自动化批量生成训练数据集流水线提示工程(Prompt Engineer…

Linux操作系统学习路线

本文来自Qwen2大模型: Linux操作系统的全面学习是一个渐进的过程,涵盖从基础知识到高级特性的多个阶段。以下是一份详细的Linux操作系统学习路线图,包括各个阶段的学习目标、建议的学习资源和实践步骤。 1. Linux 基础知识与安装 学习目标&a…

《软件定义安全》之八:软件定义安全案例

第8章 软件定义安全案例 1.国外案例 1.1 Fortinet:传统安全公司的软件定义方案 Fortinet的软件定义安全架构强调与数据中心的结合,旨在将安全转型为软件定义的模式,使安全运维能够与数据中心的其他部分一样灵活、弹性。在Fortinet看来&…

单链表经典算法题 1

前言 学习了单链表,我们就做一些题来巩固一下。还有就是解题方法不唯一,我就只讲述为自己的方法。 目录 前言 1.移除链表元素 思路 代码 2.反转链表 思路 代码 3.链表的中间节点 思路 代码 总结 1.移除链表元素 思路 我们创建一个新的表…

GUI初步开始(matlab)

GUI初步开始(matlab) (自用笔记) 打工人艰辛速成,花几个小时从零到能用,记录下details and problems: 甲方要求:GUI界面,读下位机,找到解码后格式中所需要的…

搭建WWW服务

1.实验环境的配置 【1】设置windows虚拟机server和test网络属性 打开虚拟机的【开始】菜单->【控制面板】->【网络连接】窗口。 1. 选中【本地连接】右击鼠标,选中【属性】,打开【本地连接属性】窗口。 2. 选择【网络】页签。 3. 在【此连接使…

基于文本和图片输入的3D数字人化身生成技术解析

随着虚拟现实、增强现实和元宇宙等技术的飞速发展,对高度逼真且具有表现力的3D数字人化身的需求日益增长。传统的3D数字人生成方法往往需要依赖大量的3D数据集,这不仅增加了数据收集和处理的成本,还限制了生成的多样性和灵活性。为了克服这些挑战,我们提出了一种基于文本提…

刚刚!彬川机器人社招校招入职Verify测评素质性格测评真题原题题库更新了【含答案】

一、测评环境 温馨提示 1.本次测评包含【素质性格测评】和【Verify测评】两部分,预计用时60min,请确保作答时周围环境无干扰、网络畅通; 2.请使用电脑完成作答,建议使用以下浏览器登录:IE9.0及以上版本,火…

5. 条件和递归

5. 条件和递归 本章主要话题是if表达式, 它根据程序的状态执行不同的代码. 但首先介绍两个操作符号: 向下取整除法操作符和求模操作符.5.1 向下取整除法操作符和求模操作符 向下取整除法操作符(//)对两个数除法运算, 并向下取整得到一个整数. 假设, 一个电影的播放时长为105分…

94. 二叉树的中序遍历(Swift实现, 迭代)

题目描述 使用迭代方法解题 class TreeNode {var val: Intvar left: TreeNode?var right: TreeNode?init(_ val: Int) {self.val valself.left nilself.right nil} }func inorderTraversal(_ root: TreeNode?) -> [Int] {var result [Int]() // 用于存储中序遍历…

day37| 435. 无重叠区间 763.划分字母区间 56. 合并区间 738.单调递增的数字

文章目录 前言435. 无重叠区间思路方法一方法二 763.划分字母区间思路方法二 补充内容 重叠区间 56. 合并区间思路方法一 我自己写的方法二 教程的思路【更巧妙😶】 738.单调递增的数字思路方法一方法二 使用list、不使用flag 总结 前言 435. 无重叠区间 注意&…

【PL理论】(22) 函数式语言:多参数 | 柯里化 (Currying) : 将多参数函数实现为返回一个函数的函数

💭 写在前面:本章我们将继续讲解函数式语言,介绍多参数,着重讲解柯里化的概念,将多参数函数实现为返回一个函数的函数。 目录 0x00 多参数(Multiple Arguments) 0x01 柯里化(Curr…

【车载音视频电脑】双卡式行车记录仪,带AI识别分析,支持4路AHD 1080p高清输入

一、产品外观 外观专利设计,铝合金材质,散热好、小巧、易安装;塑胶前面板,美观简洁大方,有独立锁。 二、产品特点 支持4路AHD高清输入1080P*30FPS、720P、D1、CIF分辨率等;支持接IPC,用网口&a…