Nginx入门方面知识点

一、Nginx的简介以及应用场景

1.1 简介

1. nginx 是一款轻量级的、高性能的,并发能力强的框架
2. 可以提供HTTP服务、反向代理服务、邮箱服务等功能 
3. 由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点开发的、第一个版本是2004年10月4日发布的
4. 因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。

1.2 应用场景的应用

1. 可以应用于HTTP服务
2. 可以应用于虚拟主机
3. 可以应用于反向代理服务
4. 可以应用于负载均衡
5. 可以应用于动静分离(动态请求和静态请求的分离)

二、Nginx的安装和常用命令介绍

2.1 Nginx的安装

1)环境准备工作

-- 安装nginx需要提前准备好c语言环境
[root@qianfeng01 ~]# yum -y install gcc-c++
-- 还需要pcre和pcre-devel插件
[root@qianfeng01 ~]# yum install -y pcre pcre-devel
-- 再需要zlib压缩工具
[root@qianfeng01 ~]# yum install -y zlib zlib-devel
-- 最后还需要openssl相关工具
[root@qianfeng01 ~]# yum install -y openssl openssl-devel

2)上传、解压、更名

[root@qianfeng01 ~]# tar -zxvf nginx-1.8.0.tar.gz -C /usr/local/
[root@qianfeng01 ~]# cd /usr/local/
[root@qianfeng01 local]# mv nginx-1.8.0/ nginx

3)设置配置路径

[root@qianfeng01 ~]#  mkdir /usr/local/nginx/tmp
[root@qianfeng01 ~]#  cd /usr/local/nginx
[root@qianfeng01 nginx]# ./configure \
    --prefix=/usr/local/nginx \
    --pid-path=/usr/local/nginx/tmp/nginx.pid \
    --lock-path=/usr/local/nginx/tmp/nginx.lock \
    --error-log-path=/usr/local/nginx/tmp/error.log \
    --http-log-path=/usr/local/nginx/tmp/access.log \
    --with-http_gzip_static_module \
    --http-client-body-temp-path=/usr/local/nginx/tmp/client \
    --http-proxy-temp-path=/usr/local/nginx/tmp/proxy \
    --http-fastcgi-temp-path=/usr/local/nginx/tmp//fastcgi \
    --http-uwsgi-temp-path=/usr/local/nginx/tmp/uwsgi \
    --http-scgi-temp-path=/usr/local/nginx/tmp/scgi

3)编译并安装

[root@qianfeng01 nginx]# make && make install

注意:只要出现sbin目录,就安装成功

4)配置环境变量,并校验

[root@qianfeng01 nginx]# vim /etc/profile

#nginx environment
export NGINX_HOME=/usr/local/nginx
export PATH=$NGINX_HOME/sbin:$PATH

[root@qianfeng01 nginx]# source /etc/profile

2.2 常用命令的介绍

[root@qianfeng01 ~]# nginx -h
nginx version: nginx/1.8.0
Usage: nginx [-?hvVtq] [-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
  -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: /usr/local/nginx/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file 

注意:配置文件里默认监听的端口号是80,主机名为localhost

如何启动nginx
[root@qianfeng01 ~]# nginx

启动后,可以在windows的浏览器上访问
192.168.10.101  直接回车, 出现welcome to nginx,表示启动成功。
当然也可以在linux 使用  netstat -nltp 查看80端口是否启用。

如何关闭nginx
[root@qianfeng01 ~]# nginx  -s  stop

2.3 配置文件的介绍

配置文件分三大部分:
第一部分:是从文件开始到events之前的部分,用于nginx的全局配置

第二部分: events模块,该模块用于指定nginx与用户连接的参数

第三部分:http模块,该模块是各个应用场景配置的主配置区域。
#user  nobody;
worker_processes  1;       	 	#并发指令,用于指定nginx的并发数量

#error_log  logs/error.log;           # 错误指令,用于指定错误文件的位置和级别
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;     #pid指令,用于指定pid文件的位置


events {
    worker_connections  1024;       #连接指令,用于指定一次性可以有多少个客户端连接nginx
}


http {
    include       mime.types;      # 用于引用其他文件,相当于java源文件中的import
    default_type  application/octet-stream;     # 用于指定http处理的数据流格式

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

	# log_format 用于指定日志文件的格式,同时并起一个昵称。

    #access_log  logs/access.log  main;    #用于指定访问日志的位置和格式

    sendfile        on;     #on|off  开启和关闭
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;      #是否开启压缩  on|off

    server {
        listen       80;                # 要监听的端口号
        server_name  localhost;         # 要监听的ip或者是host

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {            #用于指定客户端的访问请求路径要访问的服务器的root
            root   html;         #用来指定root是哪里
            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;
        }

        # 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 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;
    #    }
    #}

}

三、Nginx的应用案例配置(重点)

3.1 HTTP服务

nginx本身就是一个HTTP服务器,默认配置文件监听的是80端口,ip为localhost。默认访问的静态页面的root是--prefix下的html目录。

参考配置:

server {
        listen       8089;
        server_name  192.168.10.101;
        location / {
            root   html;
            #index用于指定访问的首页是谁
            index  file1.html;             
        }
        error_page   500 502 503 504 403 /50x.html;
        location = /50x.html {
            root   html;
        }
}


并在html目录下创建file1.html文件

3.2 虚拟主机的应用

nginx可以用来配置模拟多台服务器,实现不同的访问请求。比如当客户端访问的地址如下:
192.168.10.101:8089  访问的是某一个目录下的file1.html
192.168.10.201:8089  访问的是另一个目录下的file2.html

步骤1) 配置linux的多个ip地址

[root@qianfeng01 html]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

TYPE="Ethernet"
NAME="ens33"
DEVICE="ens33"

ONBOOT="yes"
BOOTPROTO="none"

IPADDR="192.168.10.101"
IPADDR1=192.168.10.201       
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值