官方:http://nginx.org

nginx [engine x] is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server, originally written by Igor Sysoev. For a long time, it has been running on many heavily loaded Russian sites including Yandex, Mail.Ru, VK, and Rambler. According to Netcraft, nginx served or proxied 25.89% busiest sites in December 2018. Here are some of the success stories: Dropbox, Netflix, Wordpress.com, FastMail.FM.

Nginx是一个Http服务器、反向代理服务器、邮件代理服务器、通用TCP/UDP代理服务器,目前已经占据25%的市场份额。

nginx has one master process and several worker processes. The main purpose of the master process is to read and evaluate configuration, and maintain worker processes. Worker processes do actual processing of requests. nginx employs event-based model and OS-dependent mechanisms to efficiently distribute requests among worker processes. The number of worker processes is defined in the configuration file and may be fixed for a given configuration or automatically adjusted to the number of available CPU cores.

nginx有一个master进程和多个worker进程,maste进程负责读取配置和管理worker进程,worker进程负责实际的请求处理;nginx使用事件模型和操作系统特有的机制来高效在worker进程中转发请求;

一 安装

官方下载页面:http://nginx.org/en/download.html

下载编译过程如下:

# wget http://nginx.org/download/nginx-1.14.2.tar.gz
# tar xvf nginx-1.14.2.tar.gz
# cd nginx-1.14.2

# ./configure --prefix=/data/nginx-1.14.2 --with-http_ssl_module --with-stream
# make
# make install

# ls /data/nginx-1.14.2/
conf html logs sbin
# ls /data/nginx-1.14.2/sbin
nginx
# ls /data/nginx-1.14.2/conf
fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utf
fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default

configure详细参数详见官方文档:http://nginx.org/en/docs/configure.html

configure过程可能报错:

1)./configure: error: C compiler cc is not found

yum install gcc

2)./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

yum install pcre-devel

3)./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.

yum install zlib-devel

4)./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.

yum install openssl-devel

二 使用

1 配置

默认配置:$NGINX_HOME/conf/nginx.conf

如果改为其他名字,需要在启动时通过-c制定

nginx可以用作http代理(http module),也可以用作长连接代理(stream module)

1)端口

server {

listen       80;

server_name  localhost;

可以修改端口,如果是80端口,必须使用root启动,因为只有root才能使用1024以下端口;

2)反向代理--负载均衡

  upstream test_backend {
    ip_hash;
    server 192.168.0.54:8080;
    server 192.168.0.55:8080;
   }
    location / {
proxy_pass http://test_backend;

可以配置多个upstream,每个upstream指向一组后端服务器,比如一组tomcat,然后在server中制定哪些location的请求转发给哪些upstream,实现负载均衡;

2 命令

# sbin/nginx -h

nginx version: nginx/1.14.2

Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:

-?,-h         : this help

-v            : show version and exit

-V            : show version and configure options then exit

-t            : test configuration and exit

-T            : test configuration, dump it and exit

-q            : suppress non-error messages during configuration testing

-s signal     : send signal to a master process: stop, quit, reopen, reload

-p prefix     : set prefix path (default: /data/nginx-1.14.2/)

-c filename   : set configuration file (default: conf/nginx.conf)

-g directives : set global directives out of configuration file

2.1 启动

# nginx

or

# nginx -c conf/nginx.conf

启动之后进程如下:

# ps aux|grep nginx
root 3072 0.0 0.0 45852 1956 ? Ss 20:11 0:00 nginx: master process sbin/nginx -c conf/nginx.conf
nobody 3218 0.0 0.0 48396 2552 ? S 20:14 0:00 nginx: worker process

2.2 停止

# nginx -c conf/nginx.conf -s stop

2.3 刷新配置

# nginx -c conf/nginx.conf -s reload

三 登陆验证

1 安装htpasswd

# yum install httpd-tools

2 添加用户密码

# htpasswd -c /data/nginx-1.14.2/passwd test
New password:
Re-type new password:
Adding password for user test

3 添加配置在server下

auth_basic "Please input user&password";

auth_basic_user_file /data/nginx-1.14.2/passwd;

4 刷新配置

# nginx -c conf/nginx.conf -s reload

效果如下:

http代理完整配置如下:

http {

include       mime.types;

default_type  application/octet-stream;

sendfile        on;

keepalive_timeout  65;

upstream test_backend {

ip_hash;

server 192.168.0.54:8080;

server 192.168.0.55:8080;

}

server {

listen       80;

server_name  localhost;

auth_basic "Please input user&password";

auth_basic_user_file /data/nginx-1.14.2/passwd;

location / {

proxy_pass http://test_backend;

}

}

}

stream代理完整配置如下:

stream {

upstream test_backend {

server $server1:21000;

server $server2:21000;

}

server {

listen 21000;

proxy_pass test_backend;

}

}

四 限制ip访问

server {

listen       80;

server_name  test.com.internal;

location / {

allow 192.168.0.0/24;

deny all;

proxy_pass http://test_backend;

}

}

限制只有192.168.0.*网段可以访问该接口

五 以目录形式访问

    location ^~ /dir {
root /dir;
autoindex on;
}

使用module:ngx_http_autoindex_module

【原创】运维基础之Nginx(1)简介、安装、使用的更多相关文章

  1. 【Apache运维基础(1)】Apache的安装与使用

    安装 yum -y install httpd httpd-devel # 在Ubuntu里面叫做Apache2,输入localhost能打开就算成功了 额...当然专业的运维还是老老实实的去编译吧; ...

  2. 【原创】运维基础之Nginx(3)location和rewrite

    nginx location =:精确匹配(必须全部相等) ~:大小写敏感,正则匹配 ~*:忽略大小写,正则匹配 ^~:只需匹配uri部分,精确匹配 @:内部服务跳转,精确匹配 优先级: Exact ...

  3. 【原创】运维基础之yum离线环境安装软件

    首先查看系统版本号,然后根据版本号从 CentOS-7-x86_64-DVD-1708.iso 和 CentOS-7-x86_64-Everything-1708.iso 根据需要选择一个下载,我这里 ...

  4. linux运维基础知识

    linux运维基础知识大全 一,序言 每一个微不足道的知识,也是未来的铺垫.每一份工作的薪资职位,也是曾经努力的结果. 二,服务器 1,运维人员工作职责: 1)保证数据不丢失:2)保证服务器24小时运 ...

  5. linux——运维基础,与常用命令

    1 运维概述 1 什么是运维 服务器的运行维护 2 名词 IDC(互联网数据中心) 3 监控软件 zabbix(用的最多), nagios, cactti 4 常用的linux操作系统 1 CentO ...

  6. Linux运维基础

    一.服务器硬件 二.Linux的发展史 三.Linux的系统安装和配置 四.Xshell的安装和优化 五.远程连接排错 六.Linux命令初识 七.Linux系统初识与优化 八.Linux目录结构 九 ...

  7. Linux系统运维笔记(四),CentOS 6.4安装Nginx

    Linux系统运维笔记(四),CentOS 6.4安装Nginx 1,安装编译工具及库文件 yum -y install make zlib zlib-devel gcc-c++ libtool op ...

  8. 第一阶段·Linux运维基础-第1章·Linux基础及入门介绍

    01-课程介绍-学习流程 02-服务器硬件-详解 03-服务器核心硬件-服务器型号-电源-CPU 01-课程介绍-学习流程 1.1. 光看不练,等于白干: 1.2 不看光练,思想怠慢: 1.3 即看又 ...

  9. Linux系统运维基础测试题

    1    Linux运维基础测试题(第一关) 通过这段时间学习Linux基础命令,为了检测自己对Linux基础命令掌握的情况,从网上整理13到测试题,并将其整理出来供大家参考学习. 1.1    习题 ...

随机推荐

  1. 拖放排序插件Sortable.js

    特点 支持触屏设备和大部分浏览器(IE9以下的就不支持了,原因都懂得) 可以从一个列表容器中拖拽一个列表单元到其他容器或本列表容器中进行排序 移动列表单元时有css动画 支持拖放操作和可选择的文本(这 ...

  2. Java基础——枚举详解

    前言: 在第一次学习面向对象编程时,我记得最深的一句话就是“万物皆对象”.于是我一直秉承着这个思想努力的学习着JAVA,直到学习到枚举(Enum)时,看着它颇为奇怪的语法……我一直在想,这TM是个什么 ...

  3. (haut oj 1261 ) 地狱飞龙 利用不定积分求值

    题目链接:http://218.28.220.249:50015/JudgeOnline/problem.php?id=1261 题目描述 最近clover迷上了皇室战争,他抽到了一种地狱飞龙,很开心 ...

  4. [官网]Using PuTTY

    Previous | Contents | Next Chapter 3: Using PuTTY Section 3.1: During your session Section 3.1.1: Co ...

  5. redis优化

    一.配置文件优化 bind 127.0.0.1 //允许连接的ip,如果就本机连接最后127.0.0.1 protected-mode yes //是否开启保护模式.默认开启,如果没有设置bind项的 ...

  6. linux 实现centos7在线升级最新版本内核

    Kernel  (内核)是操作系统的核心,掌握所有硬件设备的控制权,也就是说,你所希望计算机帮你完成的各项工作,都需要通过内核的帮助才能完成,当然,如果我们想完成的某个功能是内核没有的,则内核不会操控 ...

  7. Django自带的用户认证auth模块

    一.介绍 基本上在任何网站上,都无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能. 使用Django,我们可以不需要自己写这些功能,因为Dj ...

  8. Qt QComboBox下拉框文字重叠解决方法

    如果QComboBox下拉框文字重叠,在设置好样式之后,在后面加 setView(new QListView())即可; m_comboRate = new QComboBox(); m_comboR ...

  9. HBase数据库配置中各配置项的释义及默认值

    2018-11-26 16:09 2018-12-20 15:44 摘自HBASE官方网站  http://hbase.apache.org/book.html#_introduction  第7.2 ...

  10. BBS 502 BadGateway 原因分析

    说明: LNMP架构. crontab里有每隔20分钟重启php的任务:然后我用python写了个每1分钟检测php-cgi进程是否存在的脚本,如果不存在则调用重启php的脚本,并邮件通知管理员.cr ...