Ubuntu16 Nginx的安装与基本配置

关于Nginx

它是一个轻量级、高性能、稳定性高、并发性好的HTTP和反向代理服务器,当咱们搭建本身的应用时,一般用它做为反向代理服务器,图片服务器和负载均衡。css

 

1.Ubuntu 16安装 Nginx

这里有2种方式能够安装:html

1.从互联网中的软件仓库安装

直接使用命令行:sudo apt-get install nginxnginx

安装好后(这里应该是帮你安装了nginx所须要的依赖库),测试:sudo service nginx start正则表达式

能够看到默认的80端口已经处于监听状态了。浏览器

注: ,能够看到,自动下载的版本较低。服务器

nginx的卸载(按顺序执行):并发

sudo apt-get remove nginx nginx-common # 卸载删除除了配置文件之外的全部文件。负载均衡

sudo apt-get purge nginx nginx-common # 卸载全部东东,包括删除配置文件。性能

sudo apt-get autoremove # 在上面命令结束后执行,主要是卸载删除Nginx的再也不被使用的依赖包。测试

sudo apt-get remove nginx-full nginx-common #卸载删除两个主要的包。

 

2.推荐去官网下载tar包,手动配置依赖环境(参考:http://www.javashuo.com/article/p-tvunbutu-gz.html

1.安装gcc g++的依赖库

sudo apt-get install build-essential(c语言的编译环境)
sudo apt-get install libtool(脚本库)

安装pcre依赖库(正则表达式库)

sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev

安装zlib依赖库(压缩解压用)

sudo apt-get install zlib1g-dev

安装SSL依赖库(16默认已经安装了)

sudo apt-get install openssl

若是没有ftp可使用rz命令上传文件。
#解压:
tar -zxvf nginx-1.14.2.tar.gz
#进入解压目录:
cd nginx-1.14.2
#编译:
make
#安装:
sudo make install
#启动:
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
注意:-c 指定配置文件的路径,不加的话,nginx会自动加载默认路径的配置文件,能够经过-h查看帮助命令。

nginx默认是80端口,输入地址便可看到nginx首页:

 

2.nginx基本配置

参考(http://www.javashuo.com/article/p-zocaqtlf-nn.html)(https://carrot.is/coding/nginx_introduction

nginx的配置文件默认位于/usr/local/nginx/conf下的nginx.conf。先看一下它的默认配置:

 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.conf配置功能的格式像css,打开nginx.conf,其中重点是做为服务器的配置的server:

 

LISTEN:

server{
  listen 80;  
}

listen表明服务器的监听端口,默认是80.通常来讲这个默认的端口不是必须的。

 

SERVER_NAME:

 server_name  localhost;

server_name也是一个很重要的参数,任意请求通过该nginx服务器,都会去匹配这个server_name对应的域名,并被匹配到不一样的server。

好比咱们能够这样设置:

    server {
        listen       80;
        server_name localhost1;

        location / {
            root   html;
            index  index1.html index1.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
       }

   }
   server {
        listen       80;
        server_name localhost2;

        location / {
            root   html;
            index  index2.html index2.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
       }
   }

上面设置了2个server。location是路由配置,index指的是该服务器的默认首页。

这样设置之后,当咱们本地配置了host,访问localhost1这个域名就会进入index1.html这个主页,访问localhost2会访问index2.html。

这个功能很强大,它可让你在一个配置文件里管理不一样的站点,使用不一样的域名,并给予它们不一样的规则。

 

 

LOCATION:

server_name  letsdoit.com;

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

 

location是一个对url访问的路由,它能够配置多个,并用正则匹配。让不一样的路径访问到不一样的应用。

好比上面的配置,location后面跟单独的"/" ,表示该地址下任意路径都匹配到其中的应用。访问www.letsdoit.com/asdf 就会匹配到这个 index.html。.

location中的“root”表明资源的路径(作图片服务器时有用)。“index”表明默认主页。

下面介绍一点经常使用的配置:(示例来源:https://blog.51cto.com/superpcm/2092317

location = / { [ configuration A ] }                     #用户请求"/"时,匹配A,例如:www.pcm.com/
location / { [ configuration B ] }                       #当用户请求"/index.html"时,匹配B,例如:www.pcm.com/index.html
location /documents/ { [ configuration C ] }             #当用户请求"/documents/"时,匹配C,例如:www.pcm.com/documents/index.html      
location ^~ /images/ { [ configuration D ] }             #当用户请求"/images/"时,匹配D,:www.pcm.com/images/1.jpg 
location ~* \.(gif|jpg|jpeg)$ { [ configuration E ] }    #当用户请求".gif|.jpg|.jpeg"时,匹配E,例如:www.pcm.com/documents/1.jpg
                                                         #上面的反斜杠是转义字符,$的意思是结尾

上面的是一些基本的配置,还有些重要的配置,当nginx作代理或者负载的时候会用到。

 

3.nginx做为图片服务器的例子

nginx做为静态资源服务器的性能也很强大,咱们经常把它做为图片服务器使用。

咱们能够利用location的规则,把单独的一个应用做为静态资源访问:

配置文件以下:

 server {
        listen       80;
        server_name  localhost;

        location /images/ {
            root   /home/ftp/;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
       }

   }

咱们以地址/imgages/ 开头的url做为资源访问路径。并经过root,作访问路径的映射:

这样配置之后,在 /home/ftp/images文件夹上传一张图片作测试:

重启nginx : ./usr/local/nginx/sbin/nginx -s reload

经过浏览器访问,测试成功:

 

注意这里新手可能会出现404的状况。我以前认为root映射的路径就是url的访问路径,

因而我这样设置:

 location /images/ {
       root   /home/ftp/images/;
 }

但这样访问的路径实际上是 /home/ftp/images/images/。

配置root的话, 访问后会在root配置的目录后跟上URL,组成一个文件路径。

若是不想在路径上加上url,可使用“alias”(参考:https://www.cnblogs.com/jiongchen/p/9139156.html

 location /images/ {
       alias /home/ftp/images/;
 }

这样就不会拼接上url。

注:

root响应的路径:配置的路径(root指向的路径)+完整访问路径(location的路径)+静态文件

alias响应的路径:配置路径+静态文件(去除location中配置的路径)

相关文章
相关标签/搜索