一、Nginx介绍

  Nginx是由俄罗斯人开发的一款高性能的Web和反向代理服务器,它也可以作为电子邮件的反向代理服务器。其以稳定、并发能力强、占用资源少等显著特点备受广大互联网公司青睐。
  Nginx作为Web服务器来使用可能大家都很清楚这一点,比如我们在发布一些静态网站的时候,通常都会选择使用Nginx,而不会去选择一些专门为动态网站提供支持的服务器如Tomcat等。作为反向代理服务器来使用也是它非常擅长的一个点,如果不了解反向代理机制的话可以去搜索一下,大致的意思是:用户发送一个请求给网站,以期获得一个响应,接收用户请求的就是Nginx服务器,但Nginx不会自己来处理这个请求,它会根据用户的不同请求类型,分发给不同的服务器去实现这个功能,然后将结果返回给用户。在这一过程,看似是向一个服务器发送请求,实则背后有无数的服务器在提供支持。
  以上,便是对Nginx的大致介绍,详细介绍还得要去他的 Nginx官网才行。
  更好地阅读体验,请移步 我的个人博客站点

二、Ubuntu下安装Nginx

  此次选用的Ubuntu版本是 Ubuntu 18.04.4 LTS。在Linux平台安装Nginx,有两种方式,其一可以通过不同Linux发行版的默认软件安装包来进行安装,其二就是自己下载Nginx源码,自行编译之后进行安装。两种方式安装的Nginx性能上并无差异,要说有差别,可能就是安装后的程序目录(例如:执行程序、配置文件的目录等)有一些差异,仅此而已。使用安装包方式安装的Nginx,其程序目录分布更符合Linux的整体系统结构的分布安排,而以自行编译方式安装的则比较自由一些。

2.1.默认安装包安装方式:

  这种方式较为简单,只需要输入几条命令,就可以完成安装。
  1)更新软件仓库源列表,使其保持最新的状态

sudo apt-get update

  2)安装Nginx(安装过程中会提示你同意占用内存,确认即可)

sudo apt-get install nginx

  至此,Nginx安装完成,然后再对其进行简单的配置就可以使用了。在进行配置之前,需要了解这种方式下安装的默认程序文件位置分布情况,具体如下:

1)所有的配置文件都在/etc/nginx下。
2)执行程序文件在/usr/sbin/nginx。
3)日志文件放在了/var/log/nginx中。分别是access.log和error.log
4)默认虚拟主机的目录配置在了/var/www/下面。这个目录位置的设定是在/etc/nginx/sites-available里的配置文件进行的。与虚拟主机相关的设置,都是在这里进行的,可以自行修改,重启Nginx即可生效。

  3)启动、关闭、重启Nginx服务

1)sudo systemctl start nginx
2)sudo systemctl restart nginx
3)sudo systemctl stop nginx

需要注意的地方
  如果正确配置了配置文件,启动后访问不到网站,记得查看防火墙规则,看是不是相关的端口未对外开放(Nginx默认的是80端口)。

2.2.源码安装方式:

  这种安装方式需要我们自己下载Nginx程序源码进行编译安装,相较于上一种方式来说比较繁琐,对Linux新手来说可能还会出错,好处就是配置比较灵活,自己可以设置程序的安装位置、配置文件的位置等等。
  既然我们要编译Nginx的源码,那么编译环境我们是必要的要准备好的,Nginx的编译过程会有一些依赖包,编译之前也得要下载安装好。另外,还需要注意版本的选择问题,一般选择最新的稳定发行版即可。
  1)编译环境(gcc)、依赖库(pcre,zlib,SSL)的安装(Ubuntu和其他的发行版Linux的命令可能有一些差别)

 sudo apt-get updatesudo apt-get install build-essential libtool libpcre3 libpcre3-dev zlib1g-dev openssl

  2)Nginx安装(使用wget方式下载源码,解压之后进行编译、安装)

1)下载源码至目录 /usr/local/src(Nginx源码下载目录可以自己定),并进行解压:
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -zxvf nginx-1.16.1.tar.gz
2)编译源码
cd nginx-1.16.1
./configure --prefix=/usr/local/nginx --with-http_ssl_module
( ./configure 主要作用是对即将安装的软件进行配置,检查当前的环境是否满足安装软件的依赖条件,生成makefile文件,以便你可以用make和make install来编译和安装程序。
–prefix=**,是设置安装路径.–with-http_ssl_module,使用https协议模块。默认情况下,该模块没有被构建。建立并运行此模块的OpenSSL库是必需的。)
make
3)安装程序
make install

  3)配置
  安装成功之后,Nginx的程序目录如下图所示:
  默认情况下,conf文件夹存放的是配置文件,html文件夹是默认创建的虚拟主机的网站目录,logs是存放日志文件,sbin是nginx的执行程序所在目录。
  4)启动Nginx

#方法1(任意目录)
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#方法2(切换至程序文件所在目录)
cd /usr/local/nginx/sbin
./nginx

  5)重启、关闭Nginx
  切换至 nginx的程序文件所在目录/usr/local/nginx/sbin/,执行如下命令:

./nginx -s reload
./nginx -s stop

三、Nginx配置优化

  Nginx安装完毕之后,在使用之前,我们得要先进行一番配置后才可以使用。配置优化的目的是让Nginx尽可能以最优的方式利用系统资源,提供高效服务。 注意,修改完配置文件,需要重启Nginx才能够生效。
  两种不同安装方式的配置文件所在位置不一样,需要按照上诉内容进行寻找。方式一的配置文件是/etc/nginx/default.conf,方式二为:/usr/local/nginx/conf/nginx.conf。
  默认的配置文件如下:


#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       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   /home/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;#    }#}}

 上面的文件的结构为:

  ...              #全局块events {         #events块,工作模式及并发量设置...
}http      #http块,主要是设定和一次HTTP请求处理有关的配置
{...   #http全局块server        #server块,设定虚拟主机配置{ ...       #server全局块location [PATTERN]   #location块{...}location [PATTERN] {...}}server{...}...     #http全局块
}

  上面的模块结构说明
  1、全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker_process数等。
  2、events块:配置影响nginx服务器与用户的网络连接处理。具体有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
  3、http块:主要是设定和一次HTTP请求处理有关的配置,可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
  4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。
  5、location块:配置请求的路由,以及各种页面的处理情况。
  以上就是这几个模块的作用所在,层次感还是很清楚的,具体到我个人的网站节点配置,配置信息如下:

#user  nobody; #运行用户,可以不进行设置
worker_processes  2;  #启动进程数量,通常设置成和cpu的数量相等#全局错误日志,这个设置可以放入全局块,http块,server块,级别依次为:debug|info|notice|warn|error|crit|alert|emerg
error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;#指定nginx进程运行文件存放位置
pid        logs/nginx.pid;#工作模式及并发量设置
events {#epoll是多路复用IO(I/O Multiplexing)中的一种方式,仅用于linux2.6以上内核,可以大大提高nginx的性能use epoll; #设置网路连接序列化,防止惊群现象发生,默认为onaccept_mutex on;  #设置一个进程是否同时接受多个网络连接,默认为offmulti_accept on;  #单个后台worker process进程的最大并发连接数。并发总数为worker_processes 和 worker_connections 的乘积,#设置了反向代理的情况下,并发总数会有所变化。worker_connections  1024;
}#http请求处理块。主要是设定和一次HTTP请求处理有关的配置
http {#文件扩展名与文件类型映射表,设定mime类型,类型由mime.type文件定义include       mime.types;#默认文件类型,默认为text/plaindefault_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 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,#对于普通应用,必须设为 on,#如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,#以平衡磁盘与网络I/O处理速度,降低系统的uptime.sendfile        on;#tcp_nopush     on;#连接超时时间,默认为75s,可以在http,server,location块。keepalive_timeout  65;#开启gzip压缩gzip  on;#设定虚拟主机配置,一个HTTP模块可以设置多个虚拟主机server {#监听的端口listen       80;#监听地址server_name  localhost;#charset koi8-r;#设定本虚拟主机的访问日志access_log  logs/host.access.log  main;#默认请求location / {#网站根目录位置root   /home/html;#定义首页索引文件的名称index  index.html index.htm;}# 定义错误提示页面error_page  404              /404.html;# redirect server error pages to the static page /50x.htmlerror_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##禁止访问 .htxxx 文件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请求处理有关的配置主要多了个SSL,其他的和HTTP差不多# 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作为静态资源的服务器、反向代理服务器来使用,目前来看,这个服务器的性能还是非常稳定的,占用的系统资源也不高,配置起来也方便。鉴于这么多优点,最近在搭建Hexo博客,于是就用了这个服务器。也就顺便总结一下和Nginx有关的一些东西。
  配置Nginx的过程中,参考学习了以下文章,在此表示感谢:

1, https://www.nginx.cn/76.html
2,https://www.cnblogs.com/knowledgesea/p/5175711.html

Ubuntu下安装Nginx服务器并进行优化相关推荐

  1. Ubuntu下安装Nginx,PHP5(及PHP-FPM),MySQL

     Ubuntu下安装Nginx,PHP5(及PHP-FPM),MySQL 2012-09-15 11:12:31 标签:php mysql ubuntu nginx php-fpm 原创作品,允许转载 ...

  2. ubuntu下安装ftp服务器

    ubuntu下安装ftp服务器 Ftp服务器是使用以vsftp为例. 1. 安装     $sudo aptitude install vsftpd     $ps aux | grep 'ftp' ...

  3. ubuntu 下安装apache服务器

    [系统版本]Ubuntu/Linaro 4.6.3-1ubuntu3 [apache版本]apache-httpd-2.2.23 [安装要点] Overview for the impatient安装 ...

  4. Ubuntu下安装tacacs+服务器

    下载tacacs+服务器地址ftp://ftp.shrubbery.net/pub/tac_plus/tacacs±F4.0.4.26.tar.gz 把压缩包拷至自己定义的文件夹中解压,# tar z ...

  5. ubuntu下安装nginx时依赖库zlib,pcre,openssl安装方法

    首先使用dpkg命令查看自己需要的软件是否安装. 例如查看zlib是否安装: dpkg -l | grep zlib 解决依赖包openssl安装,命令: sudo apt-get install o ...

  6. ubuntu 下安装nginx

    y@y:~$ sudo apt-get install nginx y@y:~$ sudo service nginx start y@y:~$ nginx默认使用80端口,打开浏览器输入:http: ...

  7. linux下安装nginx出错,Ubuntu安装Nginx服务器出错解决

    Ubuntu安装Nginx服务器错误信息: ./configure: error: the HTTP rewrite module requires the PCRE library. You can ...

  8. 【Linux】在Ubuntu下部署nginx——nginx的安装与卸载

    介绍 这里是小编成长之路的历程,也是小编的学习之路.希望和各位大佬们一起成长! 以下为小编最喜欢的两句话: 要有最朴素的生活和最遥远的梦想,即使明天天寒地冻,山高水远,路远马亡. 一个人为什么要努力? ...

  9. 检查linux中nginx是否已安装成功,linux服务下安装nginx 系统版本Ubuntu 18.04.4

    linux服务下安装nginx  系统版本Ubuntu 18.04.4 一.下载nginx包(已存入我的百度网盘) 链接:https://pan.baidu.com/s/19e4FbscqZXNrPP ...

最新文章

  1. vue.js中请求数据v-for循环使用数据
  2. java反序列化漏洞的一些gadget
  3. 帧率ffmepg 摄像头_【WIN电竞】CSGO解除锁帧方法介绍
  4. java中 queryparam_@PathParam 和 @QueryParam
  5. Mysql查漏补缺笔记
  6. Python入门之数据类型
  7. Android Glide图片加载框架(四)回调与监听
  8. 操作系统大内核和微内核_操作系统中的内核I / O子系统
  9. 12年外贸婚纱跨境老司机分享独立站推广引流实操干货
  10. 关于算法--蛮力法篇--选择排序
  11. java之final的各种用法
  12. 设计模式(一)——设计模式的七大原则
  13. c++ 11/14新特性
  14. 服务器03系统 关wmi,win10系统WMI服务器怎么关闭
  15. Unity性能优化之图形渲染(渲染性能的加强)
  16. 查看 设置mysql时区
  17. python字体有哪些_python字体推荐 python编程100例
  18. react 实现tab切换 三角形筛选样式
  19. 二维码介绍及二维码的Python生成
  20. 记录自己从零开始创建个人小程序到发布的大致过程

热门文章

  1. 一路风雨走过来:那些我亲密接触过的项目
  2. 【注意】LeetCode 2. Add Two Numbers
  3. 关于并查集的Python实现代码
  4. 【机器学习】一些模型的位置总结
  5. 以太网最小帧长与TCP/IP的联合运用
  6. 关于Decision in process状态时间变化的解释
  7. 总结(5)--- Numpy和Pandas库常用函数
  8. ovs ovn 学习资料
  9. 【bzoj2834】回家的路 分层图最短路
  10. 东北大学 16春学期《实用写作》在线作业1-3 答案