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

news2024/10/6 8:27:14

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.coloradmin.cn/o/1830180.html

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

相关文章

ReactNative和Android通信

初始化一个RN项目以后,接下来想要让Android与React Native通信 写一个继承自ReactContextBaseJavaModule类的子类,重写getName方法 package com.awesomeprojectimport android.util.Log import android.widget.Toast import com.facebook.react.bridge.…

Java并发自测题

文章目录 一、什么是线程和进程?线程与进程的关系,区别及优缺点?二、为什么要使用多线程呢?三、说说线程的生命周期和状态?四、什么是线程死锁?如何预防和避免线程死锁?五、synchronized 关键字六、并发编程的三个重要特性七、JMM (Java Memory Mod…

Android 自定义View

我们所有的试图都是起源于自定义View,包括ViewGroup也是继承于它,可以说它是视图组件之父。 我们可以从它的大致流程来分为四个部分: 构造方法,onMeasure,onLayout,onDraw 构造方法: 它主要有…

Java | Leetcode Java题解之第160题相交链表

题目: 题解: public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {if (headA null || headB null) {return null;}ListNode pA headA, pB headB;while (pA ! pB) {pA pA null ? headB : pA.next;pB …

Springmvc接收请求参数

如果你觉得这种限制很麻烦,你可以改为String 因为所有参数在接收的时候原值都是字符串

Mac | 崩溃分析

一、dump分析 1. 导入符号: ./import_pdb.sh libmedia_stream_ext.dylib.dSYM ./import_pdb.sh libowcr.framework.dSYM 2. 分析dump: ./analyze_dump.sh AE59D64F-0E1D-4A18-8DAF-C2C4D22F9FA6.dmp 3. 第 2 步骤 中会输出崩溃模块、崩溃线程及堆栈…

使用 Python 进行测试(5)测试的类型

总结 和我一起唱! 冒烟测试,让你快速失败; 回归测试,不打破过去; 健全性检查,保留所拥有; 集成测试,处理副作用; 端到端,永无尽头! 回测&#xf…

SwiftUI 6.0(Xcode 16)全新 @Entry 和 @Previewable 宏让开发妙趣横生

概览 如火如荼的 WWDC 2024 已进入第五天,苹果开发平台中众多海量新功能都争先恐后的喷薄欲出。 在这里就让我们从中挑两个轻松有趣的新功能展示给小伙伴们吧:它们分别是 全新的 Entry 和 Previewable 宏。 在本篇博文中,您将学到如下内容&a…

用React编写一个密码组件表单

theme: condensed-night-purple highlight: atelier-cave-light 背景介绍 我们在使用网站或者应用程序的登录界面或创建帐户界面时,往往避免不了需要用户输入密码这一步骤,而用户是否可以选择看见他们输入的密码是十分重要的一项功能。尤其是在当输入的…

Focusky是什么软件

Focusky是一款基于HTML5技术的多媒体演示软件,可以轻松地制作出生动有趣的PPT演示文稿、动画宣传片以及微课。与其他软件相比,Focusky拥有丰富的多媒体资源和动画效果,可以让演示内容更加生动。本文将为您详细介绍Focusky软件的功能&#xff…

awd工具安装

fscan(漏洞扫描) 下载 下载地址: Releases shadow1ng/fscan GitHub 把下载的文件放到指定文件目录里, 在文件的位置打开cmd 输入 fscan64.exe -h 192.168.1.1/24 ok了 接下来说说fscan的使用 使用 1.信息搜集: 存活探测(icmp) 端口扫描 2.爆破功能: 各类服务爆破(…

MongoDB~高可用集群介绍:复制集群(副本集)、分片集群

背景 MongoDB 的集群主要包括副本集(Replica Set)和分片集群(Sharded Cluster)两种类型。 副本集 组成:通常由一个主节点(Primary)和多个从节点(Secondary)构成。 功…

UniVue更新日志:使用ObservableList优化LoopList/LoopGrid组件的使用

github仓库 稳定版本仓库:https://github.com/Avalon712/UniVue 开发版本仓库:https://github.com/Avalon712/UniVue-Develop UniVue扩展框架-UniVue源生成器仓库:https://github.com/Avalon712/UniVue-SourceGenerator 更新说明 如果大家…

Django REST framework视图集与路由详解:深入理解ViewSet、ModelViewSet与路由映射器

系列文章目录 Django入门全攻略:从零搭建你的第一个Web项目Django ORM入门指南:从概念到实践,掌握模型创建、迁移与视图操作Django ORM实战:模型字段与元选项配置,以及链式过滤与QF查询详解Django ORM深度游&#xff…

【java】数学运算考试系统

目录 一、登录界面: 二、管理员界面: 三、学生考试界面: 面向小学低年级学生,随机生成两个整数的加减法算式要求学生解答。要求有用 户登录、注册等 GUI 界面,用户数据存入文件,体现面向对象编程思想。 …

windows11子系统Ubuntu 22.04.4子安装图形化界面

1、windows11家庭版本设置 打开虚拟机安装许可 2、Microsoft Store下载安装ubuntu 我使用的是22.04.4 LTS版本 3、 打开ubuntu 命令窗口 1、打开win11的命令行,在下拉三角下标,打开,可以看到有Ubuntu 的选项,点击即可进入linux命…

【Android面试八股文】你说一说什么是双亲委托机制?为什么需要双亲委托机制?

一、双亲委托机制 1.1 双亲委托机制概述 双亲委托机制是指当一个类加载器收到一个类加载请求时, 该类加载器首先会把请求委派给父类加载器。 如果父类加载器还存在父类加载器,则会一直向上委派,直至最终交由顶层的启动类加载器完成类加载, 每个类加载器都是如此,只有在所…

RIP解决不连续子网问题

#交换设备 RIP解决不连续子网问题 一、不连续子网的概念 相同主网下的子网,被另一个主网分割,例如下面实验拓扑在某公司的网络整改项目中,原先R1 和RS 属于同一主网络 10.0.0.0/8,现被 R2、R3、R4 分离,整网采用了 …

Docker 安装 MySQL5.7 和 MySQL8

文章目录 安装 MySQL5.7拉取镜像前期准备启动容器 安装MySQL8.0拉取镜像查看镜像前期准备启动容器 安装 MySQL5.7 拉取镜像 docker pull mysql:5.7拉下来镜像后 执行 docker images 此时我们已经有这个镜像了。 前期准备 在根目录下创建 app , 在 app 目录下创建…

牛客周赛 Round 47 解题报告 | 珂学家

前言 题解 这真的是牛客周赛? 哭了 欢迎关注 珂朵莉 牛客周赛专栏 珂朵莉 牛客小白月赛专栏 A. 小红的葫芦 签到题 但是写起来有点变扭,方法应该蛮多的 统计分组 有2组一组长度为2,一组长度为3 def check(arr):arr.sort()if arr[0] …