Nginx的配置详解

Nginx的配置详解

Nginx是一款轻量级的HTTP服务器,采用事件驱动的异步非阻塞处理方式框架,这让其具有极好的IO性能,时常用于服务端的反向代理和负载均衡。

Nginx的优点

  • 支持海量高并发:采用IO多路复用epoll。官方测试Nginx能够支持5万并发链接,实际生产环境中可以支撑2-4万并发连接数。
  • 内存消耗少:在主流的服务器中Nginx目前是内存消耗最小的了,比如我们用Nginx+PHP,在3万并发链接下,开启10个Nginx进程消耗150M内存。
  • 免费使用可以商业化:Nginx为开源软件,采用的是2-clause BSD-like协议,可以免费使用,并且可以用于商业。
  • 配置文件简单:网络和程序配置通俗易懂,即使非专业运维也能看懂。

环境:

  • VM虚拟机 Centos7.4 64位版本
  • Xshell 6终端模拟软件

1、Nginx版本说明

  • Mainline version :开发版,主要是给广大Nginx爱好者,测试、研究和学习的,但是不建议使用于生产环境。
  • Stable version : 稳定版,也就是我们说的长期更新版本。这种版本一般比较成熟,经过长时间的更新测试,所以这种版本也是主流版本。
  • legacy version : 历史版本,如果你需要以前的版本,Nginx也是有提供的。

我的系统已经安装了Nginx,可以使用如下命令进行版本检测:

[root@localhost ~]# nginx -v

如果出现以下内容说明nginx安装成功

[root@localhost ~]# nginx -v
nginx version: nginx/1.14.0

如果你的linux系统中没有安装nginx,或者不是最新的版本,那我们可以自行配置yum源,下面是官网提供的源,我们可以放心使用。

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1

复制上面的代码,然后在终端里面输入:

vim /etc/yum.repos.d/nginx.repo

然后把代码复制进去,这里需要vim操作。赋值完成后,你需要修改一下对应的操作系统和版本号,因为我的是centos和7的版本,所以改为这样。

baseurl=http://nginx.org/packages/centos/7/$basearch/

你可以根据你的系统或需要的版本进行修改。如果都已经准备好了,那就可以开始安装了,安装的命令非常简单:

yum install nginx

安装完成后,你就可以来检测一下nginx版本了。

2、Nginx基本配置文件详解

安装完成nginx之后,想知道系统中多了哪些文件,安装到了哪里,我们可以使用下面的命令进行查看:

rpm -ql nginx

rpm 是linux的rpm包管理工具,-q 代表询问模式,-l 代表返回列表,这样我们就可以找到nginx的所有安装位置了。

[root@localhost ~]# rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/modules
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/rc.d/init.d/nginx
/etc/rc.d/init.d/nginx-debug
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/sbin/nginx
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.14.0
/usr/share/doc/nginx-1.14.0/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx

nginx.conf文件解读

nginx文件是Nginx的总配置文件,在我们搭建服务器时经常调整的文件。

使用如下命令打开nginx.conf文件

vim /etc/nginx/nginx.conf

下面是文件的注释

# 运行用户,默认是nginx,可以不进行设置		
user  nginx;
#Nginx进程,一般设置和cpu核数一样
worker_processes  1;

#错误日志存放位置
error_log  /var/log/nginx/error.log warn;
#进程pid存放位置
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;#单个后台进程的最大并发数
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;#nginx访问日志的存放位置

    sendfile        off;#是否开启高效传输模式 on开启 off关闭
    #tcp_nopush     on;#减少网络报文段的数量

    keepalive_timeout  65; #保持连接的时间,也叫超时时间

    #gzip  on;#开启gzip压缩模式

    include /etc/nginx/conf.d/*.conf;#包含的子配置项的位置和文件
}

default.conf配置项详解

在nginx.conf配置项文件里面的最后一行,我们打开inclue子文件目录里面都是些什么内容,里面有些配置文件是我自己新建的。

[root@localhost conf.d]# ls
default.conf  default.conf.bak  d
efault.conf.rpmnew  quickapp-local.conf  ssl.conf  test-8081.conf  test-8082.conf  theme.crt  theme.csr  theme.key  theme_nopass.key

然后使用cat default.conf进行查看

server {
    listen       80;   #配置监听端口
    server_name  localhost;  //配置域名
    #charset koi8-r;     
    #access_log  /var/log/nginx/host.access.log  main;
    location / {
        root   /usr/share/nginx/html;     #服务默认启动目录
        index  index.html index.htm;    #默认访问文件
    }
    #error_page  404              /404.html;   # 配置404页面
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;   #错误状态码的显示页面,配置后需要重启
    location = /50x.html {
        root   /usr/share/nginx/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;
    #}
}

明白了这些配置项,我们知道我们的服务目录放在了/usr/share/nginx/html下,可以使用命令进入看一下目录下的文件。

[root@localhost html]# ls
50x.html  index.html

到这里我们的nginx服务器已经可以为html提供服务器了。我们可以打开浏览器,访问ip地址试一试。

3、Nginx服务启动、停止、重启

启动nginx服务

默认情况下,nginx是不会自动启动的,需要我们手动启动。在centos7版本里面,我们可以直接使用nginx命令进行启动服务,如果不行,那就要使用其他的命令启动了,我这里只使用nginx命令

nginx

输入命令后,没有任何提示,那我们如何知道Nginx服务已经启动了哪?可以使用Linux的组合命令,进行查询服务的运行状况。

[root@localhost ~]# ps aux | grep nginx
root      2056  0.0  0.4  49816  4092 ?        Ss   Nov01   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     2548  0.0  0.3  49820  3312 ?        S    Nov01   0:00 nginx: worker process                   
root      6433  0.0  0.0 103260   840 pts/0    S+   11:19   0:00 grep nginx

如果出现以上的内容,说明我们的Nginx被正常开启了。

停止Nginx服务的四种方法

立即停止服务:

nginx -s stop

这个方法强硬,无论是否在工作,都直接停止进程

从容停止服务:

nginx -s quit

这种方法较stop相比就比较温和一些了,需要进程完成当前工作后再停止。

killall杀死进程:

这种方法也是比较野蛮的,我们直接杀死进程,但是在上面使用没有效果时,我们用这种方法还是比较好的。

killall nginx

systemctl停止:

systemctl stop nginx.service

重启nginx服务:

nginx -s reopen

或者

systemctl restart nginx.service

重新载入配置文件,在修改了配置文件之后,都需要进行这个操作,才能生效

nginx -s reload

查看端口号

在默认情况下,Nginx启动后会监听80端口,从而提供HTTP访问,如果80端口已经被占用则会启动失败。我么可以使用netstat -tlnp命令查看端口号的占用情况。

由于篇幅原因,在这就不做全部展示了,这些题我已经整理成pdf文档免费分享给那些有需要的朋友,同时整理也花费了蛮多时间。

Java学习、面试;文档、视频资源免费获取

发布于 2020-01-13 15:37