Nginx的简单介绍和基本使用

一、简介

Nginx专为性能优化而开发,其最知名的优点是它的稳定性和低系统资源消耗,以及对HTTP并发连接的高处理能力,单台物理服务器可支持3000 ~ 50000 个并发连接请求。


二、 常用功能

1、反向代理
    这是 Nginx 服务器作为 WEB 服务器的主要功能之一,客户端向服务器发送请求时,会首先经过 Nginx 服务器,由服务器将请求分发到相应的 WEB 服务器。正向代理是代理客户端,而反向代理则是代理服务器,Nginx 在提供反向代理服务方面,通过使用正则表达式进行相关配置,采取不同的转发策略,配置相当灵活,而且在配置后端转发请求时,完全不用关心网络环境如何,可以指定任意的IP地址和端口号,或其他类型的连接、请求等。

2、负载均衡

     这也是 Nginx 最常用的功能之一,负载均衡,一方面是将单一的重负载分担到多个网络节点上做并行处理,每个节点处理结束后将结果汇总返回给用户,这样可以大幅度提高网络系统的处理能力;另一方面将大量的前端并发请求或数据流量分担到多个后端网络节点分别处理,这样可以有效减少前端用户等待相应的时间。而 Nginx 负载均衡都是属于后一方面,主要是对大量前端访问或流量进行分流,已保证前端用户访问效率,并可以减少后端服务器处理压力。

3、Web 缓存

     在很多优秀的网站中,Nginx 可以作为前置缓存服务器,它被用于缓存前端请求,从而提高 Web服务器的性能。Nginx 会对用户已经访问过的内容在服务器本地建立副本,这样在一段时间内再次访问该数据,就不需要通过 Nginx 服务器向后端发出请求。减轻网络拥堵,减小数据传输延时,提高用户访问速度。

三、安装

Nginx 下载地址:http://nginx.org/en/download.html
在这里插入图片描述
其中箭头指向的位置为源码包

1、解压

wget http://nginx.org/download/nginx-1.14.2.tar.gz
tar -zxvf nginx-1.14.2.tar.gz

2、创建运行用户、组

useradd -M -s /sbin/nologin nginx

-M :不建立宿主目录
-s :禁止登录Shell环境

3、编译安装nginx

yum -y install gcc pcre-devel zlib-devel
cd nginx-1.14.2
./configure --prefix=/usr/local/nginx --user=nginx  --group=nginx --with-http_stub_status_module
make
make install

with-http_stub_status_module模块用于状态统计,便于查看服务器的连接信息。
具体参数可参考"./configure --help"给出的说明

4、启动nginx

ln -s /usr/local/nginx/sbin/nginx  /usr/local/sbin/
nginx

5、nginx的运行控制


nginx -s reload 
reload 重载配置文件,不停止对用户服务的基础上重新加载配置文件。

帮助:-?-h

使用指定的配置文件:-c

指定配置指令:-g

指定运行目录:-p

发送信号:-s stop 立即停止服务,quit 优雅的停止服务,reload 重载配置文件,reopen 重新开始记录日志文件。

测试配置文件是否有语法错误: -t -T

打印nginx的版本信息,编译信息等:-v -V

四、 配置文件 nginx.conf

在nginx的配置文件 /usr/local/nginx/conf/nginx.conf 中,包括全局配置、I/O事件配置和HTTP配置三大块内容,配置语句的格式为 " 关键字 值 ; " (末尾以分号结束)。

1、全局配置

#user  nobody; //运行用户
worker_processes  1;  //工作进程数量

#error_log  logs/error.log;  //错误日志的位置
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

worker_processes可参考服务器的核心总数来设置

2、I/O事件配置

events {
	use epoll;    //使用epoll事件驱动模型
    worker_connections  1024;  //每个 work process 支持的最大连接数为 1024
}

3、HTTP配置


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;   // 访问日志位置

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;   //连接保持超时

    #gzip  on;

    server {
        listen       80;     //监听地址或端口
        server_name  localhost;  //网站名称

        #charset koi8-r;       //网页的默认字符集

        #access_log  logs/host.access.log  main;

        location / {             //根目录配置
            root   html;         //网站根目录的位置,相对于安装目录  
            index  index.html index.htm;        //默认首页
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;  //根据状态码返回页面
        location = /50x.html {    //返回页面配置
            root   html;
        }
		#Nginx反向代理配置
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

#关于HTTPS的配置

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

反向代理

https://blog.csdn.net/linlin_0904/article/details/89633150

负载均衡

https://www.cnblogs.com/ysocean/p/9392912.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值