Linux运营维护(简称运维)

这里是简单的使用介绍:

参考:http://einverne.github.io/post/2017/06/ubuntu-debian-install-nginx.html  (安装和简介)

Nginx 配置参考文:  http://einverne.github.io/post/2017/10/nginx-conf.html

《Nginx》教程:https://www.oschina.net/translate/nginx-tutorial-basics-concepts?cmp


Nginx 是非常流行的 HTTP/HTTPS 服务器软件,它也可以作为反向代理服务器,邮件代理服务器,可以用于负载均衡,缓存等等。

基本的Nginx 由 master 进程和 worker 进程组成, master 读取配置文件,并维护 worker 进程,而 worker 会对请求进行处理。

Configuration File’s Structure

Directive指令分为单纯指令simple和block。

simple directive,包含名称和参数名,以分号结束,比如

gzip on;

block通常声明一个作用域,比如 server 块

server {
listen 80;
}

如果block内有其他指令,则称为a context(上下文),如 events, http, server, location.

在配置文件中,放在任何contexts外面的指令directive被看成是位于main context内。

event, http等指令directive, 位于main context旁, server 可以在 http内, location 可以在 server内。

Serving Static Content 服务分发静态内容

一个重要的web server task是serving out files.(分发文件,如图片或静态HTML pages).

比如根据请求request,文件从不同的本地目录/data/www, /data/images被分发served。这需要编辑配置文件并在http block内建立一个server block和2个location blocks.

Setting Up a Simple Proxy Server

nginx最频繁的用途之一是建立一个代理服务器proxy server, 意味着a server接受请求,传递 请求到代理服务器,再从代理服务器取回响应,并发送到客户端。

首先电影代理服务器,添加一个server block到nginx's的配置文件:

server {
listen ;
root /data/up1; location / {
}
}

这是一个简单的服务器,它监听端口8080(previously, the listen directive has not been specified since the standard port 80 was use),并在本地的文件系统上,遍历所有请求到/data/up1目录。

创建这个目录并把index.html文件放入。

注意:root指令被放在server 上下文中。 Such root directive is used when the location block selected for serving a request does not include own root directive.

然后,第一个location block,把proxy_pass指令和协议htttp,名字localhost,代理服务端口8080放入参数中。

server {
location / {
proxy_pass http://localhost:8080;
} location /images/ {
root /data;
}
}

修改第2个location block, 它会遍历请求(在/data/images目录内,找匹配请求的images)

location ~ \.(gif|jpg|png)$ {
root /data/images;
}

参数 \.(gif|jpg|png)$ 是一个正则表达式,匹配所有以.gif | .jpg | .png结尾的image。

正则表达式前面加 ~, 代表这是一个正则表达式。

当nginx选择一个location block来server a request, 它首先检查location指令的指定前缀prefixes,

然后检查正则表达式。如果有一个匹配存在,则nginx pick 这个location,否则它pick the one remembered earlier.

这个代理服务器的配置结果是:

server {
location / {
proxy_pass http://localhost:8080/;
} location ~ \.(gif|jpg|png)$ {
root /data/images;
}
}

这个服务server 会过滤结尾是.gif /.jpg/.png的请求,并在/data/images目录遍历这些请求(通过增加URI到root目录的参数), 然后传递所有其他的请求到上面配置的代理服务器。

最后还要发送reload 到nginx,来应用这个新的配置



《Nginx》教程:

  • 基础概念——你可以了解命令(directive)与环境(context)的区别、继承模式,以及 Nginx 选择服务器区块的顺序,还有安装位置。

  • 性能管理——提升速度的诀窍。我们将会讲解 gzip、缓存、缓冲区以及超时设置。

  • SSL 设置——讲解用 HTTPS 来提供内容的设置步骤。


1. 基础概念

service nginx status : 查看状态。

nginx {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}

nginx 的配置文件,默认的位置包括:

  • /etc/nginx/nginx.conf,

  • /usr/local/etc/nginx/nginx.conf,或

  • /usr/local/nginx/conf/nginx.conf


指令类型

有三种类型的指令,每种都有自己的继承模型。

  • Normal:  每个上下文只有唯一值
  • Array:  同一个context可以有多个。
  • Action directive:改变事情。根据模块的需要,它继承的行为可能会有所不同。⬇️案例:
server {
rewrite ^ /foobar; location /foobar {
rewrite ^ /foo;
rewrite ^ /bar;
}
}

如果用户取地址/sample:

  1. server rewrite 指令被执行,取地址指向/foobar;
  2. 然后用location /footbar,进行匹配;
  3. location内的第一个rewrite执行,重写:从/foobar 到/foo;
  4. location内的第二个rewrite执行,重写: 从/foo到/bar;

Processing request

在 Nginx 内部,你可以指定多个虚拟服务器virtual servers,每个虚拟服务器用 server{} 上下文描述

server {
listen *: default_server;
server_name netguru.co; return "Hello from netguru.co";
} server {
listen *:;
server_name foo.co; return "Hello from foo.co";
}

这将告诉 Nginx 如何处理到来的请求。

  • 首先,nginx用listen指令来过滤一下,找对应的端口。
  • 然后,server_name指令的值,会检测请求中的Host头部信息(主机域名)
  • 最后,根据匹配结果,选择用哪个虚拟主机。(关于匹配有优先级设置,具体看教程)

server_name 指令

server_name指令接受多个值。它还处理通配符匹配和正则表达式。

关于匹配有优先级设置,具体看教程)

listen 指令

在很多情况下,能够找到 listen 指令,接受IP:端口值。

listen ;           # by default all ips are used

listen [::]:;      # IPv6 addresses

Minimal configuration

With all that knowledge - we should be able to create, and understand minimal configuration needed for running nginx.

# /etc/nginx/nginx.conf

events {}             # events context needs to be defined to consider config valid

http {
server {
listen ;
server_name netguru.co www.netguru.co *.netguru.co; return "Hello";
}
}

root, location, 和 try_files 指令

root指令

root 指令设置请求的根目录,允许 nginx 将传入请求映射到文件系统。

根据给定的请求,指定 nginx 服务器允许的内容。

#Rails app的root目录:
root /home/deploy/vue-todolist/current/public;
/current/public$ ls
.html apple-touch-icon-precomposed.png favicon.ico system
.html apple-touch-icon.png packs uploads
.html assets robots.txt

location directive

sets configuration, depending on requested URI

location [modifier] path
#例子:
location /foo/ {
# ...
}

如果没有指定修饰符,则路径被视为前缀,其后可以跟随任何东西。

Ubuntu/Debian nginx 简介的更多相关文章

  1. Ubuntu 安装nginx

    https://www.nginx.com/resources/admin-guide/load-balancer/ https://github.com/gplessis/dotdeb-nginx/ ...

  2. MySQL For Linux(CentOS/Ubuntu/Debian/Fedora/Arch)一键安装脚本(5.1-8.0)

    简介 很多童鞋不懂这么在Linux系统安装MySQL,网上大多数教程较复杂,不太适合小白安装,本教程提供一键安装脚本供大家使用,教大家怎么在Linux操作系统( 支持CentOS/Ubuntu/Deb ...

  3. virtualbox搭建ubuntu server nginx+mysql+tomcat web服务器1 (未完待续)

    virtualbox搭建ubuntu server nginx+mysql+tomcat web服务器1 (未完待续) 第一次接触到 linux,不知道linux的确很强大,然后用virtualbox ...

  4. ubuntu下nginx+php5的部署

    ubuntu下nginx+php5环境的部署和centos系统下的部署稍有不同,废话不多说,以下为操作记录:1)nginx安装root@ubuntutest01-KVM:~# sudo apt-get ...

  5. ubuntu 重启 nginx 失败,* Restarting nginx nginx ...fail!

    ubuntu 重启 nginx 失败,* Restarting nginx nginx ...fail!       执行 nginx 重启服务时,提示失败如下: $ sudo service ngi ...

  6. Nginx简介及配置实用

    Nginx简介 Nginx是一个高性能的HTTP和反向代理服务器: 支持的操作系统众多,windows.linux. MacOS X: 可实现负载均衡: Rewrite功能强大: 电商架构大部分都采用 ...

  7. ubuntu server nginx 安装与配置

    ubuntu server nginx 安装与配置 一:关于nginx http://wiki.ubuntu.org.cn/Nginx http://nginx.org/cn http://wiki. ...

  8. Nginx高性能服务器安装、配置、运维 (1) —— Nginx简介

    一.Nginx 简介 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,同时也是一个 IMAP/POP3/SMTP 代理服务器. Nginx特点 ...

  9. Ubuntu中Nginx的安装与配置

    原文地址:http://www.cnblogs.com/languoliang/archive/2013/04/01/nginx.html 1.Nginx介绍 Nginx是一个非常轻量级的HTTP服务 ...

随机推荐

  1. python --- 08 文件操作

    一.   文件 f = open(文件路径,mode = '模式',encoding = '编码格式') 1.基础 ① 读写时,主要看光标的位置 ②操作完成要写    f.close( ) f.flu ...

  2. Linux服务器搭建Nexus-Maven私服(适合新手比较基础)

    背景 在使用maven构建项目的时候,几乎都会涉及到一个“私服”的概念,那么到底什么是私服?使用私服有能够带来哪些益处? 私服:私服是指私有服务器,是架设在局域网的一种特殊的远程仓库,目的是代理远程仓 ...

  3. 集训总结DAY.1(18.5.22)——KMP

    DAY 1——5.22 in the morning 依稀记得我们有一场contest. at night chf大佬讲KMP,先膜一波~~~ luoguP3375KMP模板题 KMP算法,又称模式匹 ...

  4. 使用kubeadm 安装 kubernetes 1.12.0

    目录 简介: 架构说明: 系统配置: 1.1 关闭防火墙 1.2 禁用SELinux 1.3 关闭系统Swap 1.4 安装docker 使用kubeadm部署Kubernetes: 2.1 安装ku ...

  5. MySQL 命令操作数据表

    MySQL 命令操作数据表 1.查看表信息 desc hs_user_credit_info; 2.新增表字段 alter table hs_credit_order add search_relat ...

  6. 剥开比原看代码13:比原是如何通过/list-balances显示帐户余额的?

    作者:freewind 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchai ...

  7. 清除浏览器CSS样式

    /* YUI 3.18.1 (build f7e7bcb) Copyright 2014 Yahoo! Inc. All rights reserved. Licensed under the BSD ...

  8. 异步编程- async和await

    使用目的 避免阻塞主线程 提高程序响应能力 C#中使用 C# 中的 Async 和 Await 关键字是异步编程的核心. 疑惑 The async and await keywords don't c ...

  9. 解決 Android Studio 不停 Indexing 的問題(Updating Indices: Indexing paused due to batch update)

    遇到這個問題通常是 IDE 更新後,或是反覆使用 Android Studio 開啟其他專案所導致,解決方法其實非常簡單喔! 点击 這個選項的功用是「清除 IDE 暫存並重啟」,沒錯,會出現上述情形的 ...

  10. Panda3D

    Panda3D 是个开源的游戏及物理引擎(也支持ODE及Bullet). 相关链接:网站: https://www.panda3d.org/下载: https://www.panda3d.org/do ...