资讯专栏INFORMATION COLUMN

nginx的安装和简单使用(一)

GitChat / 1757人阅读

摘要:是一款是由俄罗斯的程序设计师所开发高性能的和反向代理服务器,也是一个代理服务器。本文主要是介绍了一些基础的的使用,环境是。指令是起到了一个路由的效果,只能在块级中使用,对于各路径和结果进行响应的设置。

nginx
Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。在高连接并发的情况下,Nginx是Apache服务器不错的替代品。

nginx的出现可以说对于那些在windows上使用IIS,linux上使用apache2的人提供了更多的选择,使用nginx的情况主要是满足了以下的一些功能:

本地代理,对于前端开发人员而言,需要把很多的请求代理到本地,本质上还是在本地使用nginx起了web服务,进而完成一些重定向工作;

web服务器,nginx可以在服务器上承担整个web服务的分发和响应,其中反向代理、负载均衡是它很重要的功能。

本文主要是介绍了一些基础的nginx的使用,环境是mac10.13.2。

安装 镜像brew

在mac上可以使用两种方法来进行:

brew命令安装

nginx源码编译安装

本文没有尝试./configure make make install的方式,不过可以看看这个安装NGINX;本文只是尝试使用brew来进行安装。

homebrew主要分两部分:git repo(位于GitHub)和二进制bottles(位于bintray),这两者在国内访问都不太顺畅。可以替换成国内的镜像,

替换git源:

替换brew.git:
cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git

替换homebrew-core.git:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

替换二进制bottles源[bash和zsh需要区分启动文件]:

//对于bash用户:
echo "export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles" >> ~/.bash_profile
source ~/.bash_profile

//对于zsh用户
echo "export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles" >> ~/.zshrc
source ~/.zshrc

如此便可以执行安装:

brew install nginx

执行完成的话那么就可以查看结果如何:nginx -h或者nginx -v看看结果

nginx -h   
nginx version: nginx/1.12.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: /usr/local/Cellar/nginx/1.12.2_1/)
  -c filename   : set configuration file (default: /usr/local/etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

nginx -v
nginx version: nginx/1.12.2
配置文件
The way nginx and its modules work is determined in the configuration file. By default, the configuration file is named nginx.conf and placed in the directory /usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx

nginx的命令比较的少,大部分配置都是在配置文件当做,配置文件的路径/usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx之中,#可以作为注释符来注释掉改行;

下面对这里的部分配置内容做个整体的了解和分类:

从形式上分类:简单指令和块级指令集
配置文件中主要是存在一些simple directives and block directives;可以认为是简单的指令和块级指令集,简单指令就是:

worker_processes  1;

块级指令集就是一个块级指令名加上{},里面包含很多简单指令集,块级指令集可以嵌套;

events {
    worker_connections  1024;
}

从功能模块分类:主模块、事件模块、其他基本模块

主模块是控制nginx的一些基本指令集合,包含了类似上述的简单指令worker_processes 1;在内的一些基本指令;

事件模块设置Nginx处理连接请求;

其他基本模块包括常用地http模块;

先看一个初始状态的配置文件:

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


events {
    worker_connections  1024;
}


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

        # 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;
    #    }
    #}
    include servers/*;
}

开始分析配置文件中的一些指令:

简单指令【本文例举了主模块的部分指令】

在默认的生成的配置文件的头部,有这么几行简单的指令,虽然大部分是被注释掉的,但是这里简单的说下其中的意义,这些简单指令都属于主模块的指令,用于控制基本的nginx的功能:

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

user这个指令名代表的是执行worker processes的本机用户,默认是nobody,那么如果需要读写一些roort或者其他用户所有权的文件时,如果当前配置文件填写的user这个指令名对应的用户又不具有r+w+x的权限时,就会出现一些权限问题;

语法: user user [group]
缺省值: nobody nobody
指定Nginx Worker进程运行用户,默认是nobody帐号。

worker_processes这个指令名是指配置worker_processes的数量,nginx启动时会有一个主进程和若干的工作进程,这个指令就是来规定工作进程的数量的,对应的是一个数值

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.  
语法: worker_processes number
缺省值: 1

error_log这个指令是来记录nginx的运行出行的一些异常,可以指定异常级别

语法: error_log file [ debug | info | notice | warn | error | crit ]
缺省值: ${prefix}/logs/error.log

pid这个是用来指定运行nginx的进程ID的;

语法: pid file

块级指令集(本文例举了http模块的部分功能)

由于很多模块都是块级指令集的形式的存在,本文拿出来http模块的部分指令来进行简单的解析;后面的第二篇会考虑把一些实用的、常用的、很有用的功能进行进一步讲解。http核心模块的指令集合、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       8080;
        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;
        }

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

include指令是主模块的指令,可以用在http的块级指令集中,是防止单个配置文件过大,可以直接引用其他的配置文件,而例子中的 mime.types是一个文件,里面主要是比较全面的MIME信息,能包含文本、图像、音频、视频以及其他应用程序专用的数据和文件后缀名的映射

sendfile指令是指是否开启linux2+的一个sendfile的功能,sendfile详解

server是http模块的重要指令,其响应http链接的关键,一般而言会包含listen server_name location这三部分。

localtion指令是起到了一个路由的效果,只能在server块级中使用,对于各路径和结果进行响应的设置。

至于https和一写其他的指令将会留到下文进行详细的学习分析。其中可以使用的指令和变量如下:

http核心模块的指令

可在http核心模块的块级指令集中使用的全局变量

运行

查看官方的文档NGINX的文档,可以通过nginx的可执行文件来启动nginx服务;

所以要启动nginx,可以这样:

$ nginx // 一般安装的时候都会放到系统的启动文件夹里面[环境变量] /usr/local/bin/nginx

在启动之后需要使用nginx -s signal来进行操作,其中signal可以使用以下一些指令:

stop — fast shutdown

quit — graceful shutdown

reload — reloading the configuration file

reopen — reopening the log files

如果要停止服务,可以这样(完成当前的所有请求后停止,和stop的区别是stop会立即停止nginx):

$ nginx -s quit

如果修改了配置文件,要重新生效,可以这样:

$ nginx -s reload

一个nginx的中文翻译网站

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/39801.html

相关文章

  • Nginx安装

    摘要:的模块使用来解析正则表达式,所以需要在上安装库,是使用开发的一个二次开发库。命令安装库提供了很多种压缩和解压缩的方式,使用对包的内容进行,所以需要在上安装库。确保系统已经安装了,如果没有安装,执行安装。这些需求也是作为一个前端所关心的。 前言 身为前端,本来是拒绝使用nginx的,想着nodeJs能够大一统。不过在反向请求代理,二级域名配置等方面还是比不上nginx。最关键的一点就是,...

    jimhs 评论0 收藏0
  • Linux安装Nginx正确方式

    摘要:使用系统二进制源方式安装在系或者系这种方式最简单的,最快捷的方式,但是不是最好的方式,下面我们来说这种主要问题。我看见网上大多数教程,都是将编译依赖直接装在这种方式并不好。安装后,可以使用配置文件中的指令更改名称。 本文出处https://shenyifengtk.github.io如有转载,请说明出处 如果你和我一样,作为一个苦逼的Java后台除了实现实现一大堆项目功能,还要兼顾项目...

    XUI 评论0 收藏0
  • Linux安装Nginx正确方式

    摘要:使用系统二进制源方式安装在系或者系这种方式最简单的,最快捷的方式,但是不是最好的方式,下面我们来说这种主要问题。我看见网上大多数教程,都是将编译依赖直接装在这种方式并不好。安装后,可以使用配置文件中的指令更改名称。 本文出处https://shenyifengtk.github.io如有转载,请说明出处 如果你和我一样,作为一个苦逼的Java后台除了实现实现一大堆项目功能,还要兼顾项目...

    freecode 评论0 收藏0
  • Nginx初探究:安装简单使用

    摘要:当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用做反向代理。两个域名是和服务器使用虚拟机实现反向代理第一步安装两个,分别运行在和端口。 showImg(http://ou3np1yz4.bkt.clouddn.com/nginx_logo1.jpg); 在学习淘淘商城的过程中接触到了nginx,今天就把使用它的过程记录下来,作为留存。 一、什么...

    ckllj 评论0 收藏0
  • nginx服务器详细安装过程(使用yum 源码包两种安装方式,并说明其区别)

    摘要:网上看别人写的服务器配置,有的是源码包安装的,有的时安装的。通过源码包编译安装的软件,通常都放在包名路径下。正则表达式使用在指令和模块中。 网上看别人写的 nginx 服务器配置 ,有的是源码包安装的,有的时 yum 安装的。如果是新手,可能会有疑问,这两种安装方式有什么区别?我应该使用哪种方式?系统里可以两个都安装可以吗?怎么卸载?等等问题,那么在这里,我做下总结,详细介绍下这两种方...

    Cc_2011 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<