什么是Nginx

是一个高性能的反向代理服务器
正向代理代理的是客户端
反向代理代理的是服务端

Apache、Tomcat、Nginx

静态web服务器
jsp/servlet服务器 tomcat

安装Nginx

1. 下载tar包  (地址:http://nginx.org/en/download.html)

安装make:

yum -y install gcc automake autoconf libtool make

  安装g++:

yum install gcc gcc-c++

遇到yum在使用,ps -ef |grep yum,查出线程id ,kill -9 id 删除,如果删除不了,就用rm -f /var/run/yum.pid 删除,还不行,重启虚拟机

2. tar -zxvf nginx.tar.gz 解压  cd 进入目录
3. ./configure [--prefix]   ./configure --prefix=/root/ngnix
4. make && make install

启动和停止

1. sbin/nginx
2. ./nginx -s stop

3. ./nginx -s reload

启动后,访问虚拟机ip地址,出现

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com. Thank you for using nginx.

  若出现 forbidden 403,则在配置中加入当前用户root,重新启动nginx即可。(若安装到root目录下,其他用户(nginx,www)----->访问路径就不可以在/root下)

nginx.conf

Main
event
http

虚拟主机配置

server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}

  

基于ip的虚拟主机
  不演示

基于端口号的虚拟主机

在nginx.conf中添加如下配置,后重新加载配置

server {
listen 8080;
server_name localhost;
location / {
root html;
index index.html;
}
}

 访问http://192.168.20.130:8080 

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com. Thank you for using nginx.

  

基于域名的虚拟主机
www.guapaoedu.com / ask.gupaoedu.com / git.gupaoedu.com / bbs.gupaoedu.com

vi conf/nginx.conf,在html文件夹下添加 bbs.html 、ask.html,reload配置

在本地C:\Windows\System32\drivers\etc 的host文件下绕过解析,添加对应ip映射

server {
listen 80;
server_name www.gupaoedu.com;
location / {
root html;
index index.html;
}
}
server {
listen 80;
server_name bbs.gupaoedu.com;
location / {
root html;
index bbs.html;
}
}
server {
listen 80;
server_name ask.gupaoedu.com;
location / {
root html;
index ask.html;
}
}

  进行访问,如果无法访问

  1.cmd打开命令窗口, ipconfig /flushdns 刷新dns解析缓存

  2.注意 输入ask.gupaoedu.com 的游览器前缀时https 还是http,必须是http才行

  http://bbs.gupaoedu.com/ 页面显示bbs.html的内容

location
配置语法
location [= | ~* | ^~ ] /uri/ {...}
配置规则
location = /uri 精准匹配
location ^~ /uri 前缀匹配
location ~ /uri
location / 通用匹配
规则的优先级

1 location = /
2 location = /index
3 location ^~ /article/
4 location ^~ /article/files/
5 location ~ \.(gif|png|js|css)$
6 location /
http://192.168.11.154/ ->1
http://192.168.11.154/index ->2
http://192.168.11.154/article/files/1.txt ->4 找最长的路径匹配(路径有重复会报错)
http://192.168.11.154/mic.png ->5

  

1. 精准匹配是优先级最高
2. 普通匹配(最长的匹配)
3. 正则匹配

实际使用建议

location =/ {
}
location / {
}
location ~* \.(gif|....)${
}

  Nginx模块

反向代理、email、nginx core。。。

模块分类

1. 核心模块 ngx_http_core_module
2. 标准模块 http模块
3. 第三方模块

ngx_http_core_module

server{
listen port
server_name
root ...
}

  location 实现uri到文件系统路径的映射

2. error_page

ngx_http_access_module

实现基于ip的访问控制功能
1、allow address | CIDR | unix: | all;
2、deny address | CIDR | unix: | all;
自上而下检查,一旦匹配,将生效,条件严格的置前

  

如何添加第三方模块
1. 原来所安装的配置,你必在重新安装新模块的时候,加上
2. 不能直接make install

configure --prefix=/data/program/nginx

  安装方法

./configure --prefix=/安装目录 --add-module = /第三方模块的目录
./configure --prefix=/root/nginx --with-http_stub_status_module --with-http_random_index_module
make
cp objs/nginx $nginx_home/sbin/nginx
需要先关闭nginx,输入y确认复制
然后看能否正常启动nginx

 http_stub_status_module

在nginx.conf中添加

location /status {
stub_status;
}

  访问:http://192.168.20.130/status

Active connections: 2
server accepts handled requests
3 3 9
Reading: 0 Writing: 1 Waiting: 1

  

Active connections:当前状态,活动状态的连接数
accepts:统计总值,已经接受的客户端请求的总数

handled:统计总值,已经处理完成的客户端请求的总数
requests:统计总值,客户端发来的总的请求数
Reading:当前状态,正在读取客户端请求报文首部的连接的连接数
Writing:当前状态,正在向客户端发送响应报文过程中的连接数
Waiting:当前状态,正在等待客户端发出请求的空闲连接数

http_random_index_module

www.gupaoedu.com
随机显示主页
一般情况下,一个站点默认首页都是定义好的index.html、index.shtml等等,如果想站点下有很多页面想随机展示给
用户浏览,那得程序上实现,很麻烦,使用nginx的random index即可简单实现这个功能,凡是以/结尾的请求,都
会随机展示当前目录下的文件作为首页
  \1. 添加random_index on 配置,默认是关闭的

location / {
root html;
random_index on;
index index.html index.htm;
}

  \2. 在html目录下创建多个html页面

Nginx(一)--nginx的初步认识及配置的更多相关文章

  1. nginx+php 在windows下的简单配置安装

    开始前的准备 PHP安装包下载:http://windows.php.net/downloads/releases/php-5.5.14-Win32-VC11-x86.zip Nginx 下载地址:h ...

  2. Nginx + Tomcat Windows下的负载均衡配置

     Nginx + Tomcat Windows下的负载均衡配置 一.为什么需要对Tomcat服务器做负载均衡?    Tomcat服务器作为一个Web服务器,其并发数在300-500之间,如果超过50 ...

  3. nginx下目录浏览及其验证功能配置记录

    工作中常常有写不能有网页下载东西的需求,在Apache下搭建完成后直接导入文件即可达到下载/显示文件的效果;而Nginx的目录列表功能默认是关闭的,如果需要打开Nginx的目录列表功能,需要手动配置, ...

  4. Linux下Nginx+Tomcat负载均衡和动静分离配置要点

    本文使用的Linux发行版:CentOS6.7 下载地址:https://wiki.centos.org/Download 一.安装Nginx 下载源:wget http://nginx.org/pa ...

  5. Flask+Nginx+uWSGI在Ubuntu服务器上的配置

    Flask+Nginx+uWSGI在Ubuntu服务器上的配置 Step1 安装系统环境 Ubuntu服务器选择是阿里云的ECS服务,ECS提供单独的内存\CPU\带宽\存储规格可以选择,并且提供合适 ...

  6. nginx 反向代理 与 Apache backend的配置联合配置

    nginx 反向代理 与 Apache backend的配置联合配置: 说明: nginx 将http映射到Apache上的特定子目录. 配置方法步骤: 1.  设置域名, 子域名映射到指定服务器ip ...

  7. 【nginx】负载均衡和proxy的配置

    简介 使用upstream模块实现nginx负载均衡使用nginx_upstream_check_module模块实现后端服务器的健康检查使用nginx-sticky-module扩展模块实现Cook ...

  8. nginx(三)初步搭建nginx虚拟主机

    上面就是nginx基于域名.ip访问的配置,掌握住格式,就很好配置了. 一.基于域名的虚拟主机的配置:1.我们在此复习一下DNS的配置:[root@mgmserver /]# hostnamemgms ...

  9. Nginx 笔记与总结(3)配置虚拟主机

    Nginx 重启的另外一种方式,相当于 kill -HUP `cat /usr/local/nginx/logs/nginx.pid`: /usr/local/nginx/sbin/nginx -s ...

随机推荐

  1. hdu 1878 欧拉回路(联通<并查集> + 偶数点)

    欧拉回路Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  2. JS三座大山再学习(一、原型和原型链)

    原文地址 ## 前言 西瓜君之前学习了JS的基础知识与三座大山,但之后工作中没怎么用,印象不太深刻,这次打算再重学一下,打牢基础.冲鸭~~ 原型模式 JS实现继承的方式是通过原型和原型链实现的,JS中 ...

  3. 使用ndk交叉编译android各平台版本的第三方库

    只要弄明白了ndk-bundle的目录结构,交叉编译的基本原理就可以自行编写脚本去编译了.从仓库拿下代码包后,一般在linux平台下编译当前平台使用的库,只要使用其自动配置脚本configure进行平 ...

  4. 程序员用于机器学习编程的Python 数据处理库 pandas 进阶教程

    数据访问 在入门教程中,我们已经使用过访问数据的方法.这里我们再集中看一下. 注:这里的数据访问方法既适用于Series,也适用于DataFrame. **基础方法:[]和. 这是两种最直观的方法,任 ...

  5. react 组件间通信,父子间通信

    一.父组件传值给子组件 父组件向下传值是使用了props属性,在父组件定义的子组件上定义传给子组件的名字和值,然后在子组件通过this.props.xxx调用就可以了. 二.子组件传值给父组件 子组件 ...

  6. html基础——div/span

    div是一个块标签/盒子标签,单独占据一行 span不会占据一块,一般是用来修改某几个文字的格式 比如一行字,需要将每一句的首字母大写,就可以使用span标签 如果是要将一个段落的字加样式,两个都可以 ...

  7. ubuntu 交叉编译 busybox 1.31.1

    目的:静态编译 Busybox_arm64 1.13.1 环境:Ubuntu 18.04.3 #----------------环境配置 # aarch64-linux-gnu-g++ sudo ap ...

  8. HT Vue 集成

    (本文中 dataModel = dm = 数据容器, gv = graphView = g2d = 2D 视图) 初始化项目 使用 vue-cli 生成项目.生成注意以下几个问题 1. 建议手动配置 ...

  9. 小白学 Python 爬虫(9):爬虫基础

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  10. 【数据结构】之顺序表(C语言描述)

    顺序表是线性表的一种,它将元素存储在一段连续的内存空间中,表中的任意元素都可以通过下标快速的获取到,因此,顺序表适合查询操作频繁的场景,而不适合增删操作频繁的场景. 下面是使用 C语言 编写的顺序表的 ...