第一步:yum安装nginx

1:添加源

sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

使用yum安装nginx yum nginx安装_配置文件

2:安装nginx

sudo yum install -y nginx

使用yum安装nginx yum nginx安装_配置文件_02

出现Complete!代表安装完成!

3:启动nginx

systemctl start nginx.service

4:查看防火墙开启的端口

firewall-cmd --zone=public --list-ports

使用yum安装nginx yum nginx安装_nginx_03

我的为空,什么端口都没开放,开放了的会有80/tcp

5:开放80端口(已开放80端口的就不用执行了)

firewall-cmd --zone=public --add-port=80/tcp --permanent

6:防火墙配置生效(已开放80端口的就不用执行了)

firewall-cmd --reload

7:再次查看开放的端口

firewall-cmd --zone=public --list-ports

使用yum安装nginx yum nginx安装_nginx_04

有了,80/tcp

8:浏览器输入ip访问

使用yum安装nginx yum nginx安装_php_05

访问成功!

注:service访问目录如果在别的盘(如/www或/home),需要关闭selinux或者设置成宽容模式(setenforce 0),不然会报403错误

#查看当前状态
systemctl status nginx.service
#启动
systemctl start nginx.service
#停止
systemctl stop nginx.service
#重启
systemctl restart nginx.service
#开机自启动
systemctl enable nginx.service
#停止开机自启动
systemctl disable nginx.service

9:nginx常用命令

#帮助
nginx -?或nginx -h

使用yum安装nginx yum nginx安装_nginx_06

列出了可以使用的命令

#查看版本,小写v
nginx -v
#查看版本以及编译选项,大写V
nginx -V

使用yum安装nginx yum nginx安装_配置文件_07

#检查配置文件
nginx -t
#检查配置文件并输出配置
nginx -T

使用yum安装nginx yum nginx安装_php_08

这个证明配置文件没有语法问题,大写T会在这下面把配置文件输出,一般用不到

#静默模式,检查配置文件,非错误信息不显示
nginx -q

使用yum安装nginx yum nginx安装_php_09

#给nginx主进程发送信号,信号有stop、quit、reopen、reload
nginx -s stop|quid|reopen|reload

stop:停止nginx服务

quit:停止nginx服务,但是需要worker进程完成现有的请求。就是不再接收新请求,然后现在已经接收的请求要处理完,然后再关闭

reopen:重新打开日志文件,可用于日志切割

reload:更改配置文件后可使用这个命令重新加载,执行过程:master进程接收到reload信号,会去重新加载解析新配置文件,如果新配置文件有错误,则继续使用上次的配置文件运行并抛出错误,如果新配置文件正确,则向worker进程发出退出信号,worker进程就会停止接收新请求,空闲的woker会立刻关闭并被master进程重新创建,有正在处理请求的worker进程需要完成请求后再关闭,再这个过程中master主进程是不关闭的,所以客户端的新请求也可以正常接收,下面运行一下试试

a:先看一下现在的nginx进程信息

b:更改nginx配置文件nginx.conf,将pid改成pidd,这样配置文件就是错误的,改完后reload,会输出错误信息

c:查看nginx进程信息,发现master和worker进程并没有发生变化,证明并没有重载,还是和执行reload前一样

d:将nginx.conf改回来并reload,没有错误输出说明成功了,再次查看nginx进程,发现master进程的pid没变,但是worker进程的pid变了

使用yum安装nginx yum nginx安装_nginx_10

 

#设置前缀路径
nginx -p prefix

 

#指定错误日志路径,默认/var/log/nginx/error.log
nginx -e filename

 

#指定配置文件路径,默认/etc/nginx/nginx.conf
nginx -c filename

 

#指定全局指定配置文件
nginx -g directives

 10:配置文件

  配置文件是根目录下的nginx.conf,内容可分为main全局段、events段、http段、server段、upstream段、location段;下面大概介绍一下每个段主要配置的内容

  【main全局段】 

    介绍:用于配置用户,进程,错误日志等相关参数

    常用参数:

      user nginx;#worker进程身份

      worker_processes 4;#worker进程数量,一般与cpu核心数相等(lscpu可查看cpu信息),建议不超过cpu核心的2倍,默认auto(会自动去匹配cup核心数)

      worker_cpu_affinity 1000 0100 0010 0001;#worker进程绑定CPU核心,cpu是几核就用几个0代表(对应位置0代表不使用,1代表使用),几个worker进程就写几个(用空格隔开)

      error_log filename;#全局错误日志

      pid nginx.pid;#指定pid文件(用来存放nginx主控进程的进程号)

  【events段】

    介绍:用于配置IO模型(如epoll、kqueue、select或poll)、work进程连接数等

    常用参数:

      #事件驱动

      use epoll;

      #每个worker进程的最大连接数,默认最大是1024,可以根据cpu的使用程度来具体调整,同时操作系统的“进程最大可打开文件数”也会限制最大值。

      #每个请求会占用worker的2个或4个连接数,静态访问占用2个,http请求占用4个

      #所以支持的最大并发数也就是【静态worker_connections * worker_processes /2】【http请求worker_connections * worker_processes /4】。

      worker_connections 1024;

  【http段】

    介绍:http相关模块支持,内含server、upstream

    常用参数:

      #nginx支持的媒体类型库文件

      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  /var/log/nginx/access.log  main;#main就是我们上面定义的日志格式名称

      #超时时间

      keepalive_timeout 60;

      #默认编码

      charset utf-8;

      #设定通过nginx上传文件的大小

      client_max_body_size 100m;

      #隐藏nginx版本号

      server_tokens off;

 

      #开启高效文件传输模式(也可设置在server里)

      sendfile on;

      tcp_nopush on;

      tcp_nodelay on;#on:发送报文不延时不管数据包多小都及时发送;off:会等到一定量的数据报文一起发送

 

      #gzip压缩优化,访问加快,消耗cpu多,纯文本压缩率高,但要大于1kb,不然可能越压越大,图片视频压缩率低,也可能越压越大(也可设置在server里)

      gzip on;#开启压缩

      gzip_min_length 1k;#压缩的页面最小字节

      gzip_buffers 432k;#压缩缓存区大小

      gzip_http_version 1.1;#压缩版本

      gzip_comp_level 9;#压缩比率

      gzip_types text/css text/xml application/javascript;#指定压缩的类型

  【server段】

    介绍:配置虚拟主机,包含location段

    参数:

#监听端口
      listen 80;#443 ssl
      #域名,可多个,用空格隔开
      server_name www.aaa.com
      #访问目录
      root /www
      #默认起始页
      index index.php index.html
      #设置https     
      ssl on;#启用ssl功能
          ssl_certificate  /etc/nginx/ssl/admin.zkzgh.com/admin.zkzgh.com.pem;#证书
          ssl_certificate_key  /etc/nginx/ssl/admin.zkzgh.com/admin.zkzgh.com.key;#证书私钥文件
          ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
          ssl_protocols TLSv1 TLSv1.1 TLSv1.2;#支持的ssl协议版本
      ssl_session_timeout 5m;#客户端一侧的连接可以复用ssl session cache中缓存 的ssl参数的有效时长;
          ssl_prefer_server_ciphers on;
      #http转https    
      server {
            listen 80;
            server_name www.aaa.com;
            rewrite ^(.*) https://www.aaa.com$1 permanent;
      }

  【upstream段】

    介绍:只能用于http配置段中,意思是定义一组后端服务器组

    示例:

upstream name {
        server www.a.com;
        server www.b.com weight=2;#weight是权重,默认是1
      }

  【location段】

    介绍:url匹配到实际,优先级(=, ^~, ~/~*,不带符号)

#示例:
      # 不存在的文件转发到index.html
      location / {
                    if (!-e $request_filename) {
                          rewrite ^(.*)$ /index.php$1 last;
                          break;
                    }
              }      #设置错误页面    
      error_page 500 502 503 504 /50x.html;
              location = /50x.html {
                    root   html;
              }
      #php请求发送到phpfpm   
      location ~ \.php(.*)$ {
                    fastcgi_pass   127.0.0.1:9000;
                    fastcgi_index  index.php;
                    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                    fastcgi_param  PATH_INFO $1;
                    include        fastcgi_params;
                client_max_body_size                  20m;
                client_body_temp_path   /tmp/nginx_tmp;
          }
      #转发“域名/api/url”到“http://api”,可以和upstream段联合使用
      location ^~/api {
                   proxy_set_header Host $host;
                   proxy_set_header X-Real-IP $remote_addr;
                   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                   proxy_buffering off;
                   rewrite ^/api/(.*)$ /$1 break;
                   proxy_pass http://api;
            }      #upstream api {
            #server http://www.api.com;
      #}      #查看nginx状态
      location /status {
        stub_status on;
        access_log off;
        allow 192.168.100.0/24;
        deny all;
      }
      #防盗链
      location ~* \.(rmvb|jpg|png|swf|flv)$ {
        #上面后缀的文件实行防盗链
        valid_referers none blocked www.a.com;
        #表示对www.a.com此域名开通白名单,多个用空格隔开
        if ($invalid_referer) {
          #如果请求不是从www.a.com白名单发出来的请求,直接重定向到403.html这个页面或者返回403
          #rewrite ^/ 404.jpg;
          return 403;
        }
      }
      #防爬虫
      if ($http_user_agent ~* LWP:Simple|BBBike|wget) {
        return 403 ;
        rewrite ^(.*) http://www.a.com/$1 permanent;
      }
 
      #禁止ip访问
      location / {
        allow 202.111.12.211;
        deny all;
      }
      #不记录不需要的日志
      location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$ {
        access_log off;
      }