nginx

一、 Nginx 简介

  Nginx 是由俄罗斯软件工程师 Igor Sysoev 开发的一个高性能的 HTTP 和反向代理服务器,具备 IMAP/POP3 和 SMTP 服务器功能。

    作为 Web 服务器:相比较与 Apache, Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使 Nginx 尤为受到虚拟主机提供商的欢迎,能够支持高达 50000 个并发的连接数的响应。

    作为负载均衡服务器: Nginx 既可以在内部直接支持 Rails 和 PHP,也可以支持作为 HTTP代理服务器对外惊醒服务, Nginx 用 C 语言编写,不论是系统资源开销还是 CPU 使用效率都比 Perlbal 要好的多。

    作为邮件代理服务器: Nginx 同时也是一个非常优秀的邮件代理服务器。(最早开发这个产品的目的之一也是作为邮件代理服务器)

二、CentOS 7下安装部署
  配置epel yum 源
    wget http://dl.Fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 
    rpm -ivh epel-release-latest-7.noarch.rpm
    yum install nginx -y


查看确认 是否安装
  root@localhost ~]# rpm -qa | grep nginx
    nginx-1.10.2-1.el7.x86_64
    nginx-mod-stream-1.10.2-1.el7.x86_64
    nginx-mod-http-geoip-1.10.2-1.el7.x86_64
    nginx-all-modules-1.10.2-1.el7.noarch
    nginx-mod-http-perl-1.10.2-1.el7.x86_64
    nginx-mod-http-image-filter-1.10.2-1.el7.x86_64
    nginx-mod-mail-1.10.2-1.el7.x86_64
    nginx-filesystem-1.10.2-1.el7.noarch
    nginx-mod-http-xslt-filter-1.10.2-1.el7.x86_64


查看 安装nginx 所生成的文件 
  [root@localhost ~]# rpm -ql nginx 
    /etc/logrotate.d/nginx

  /etc/nginx/fastcgi.conf
  /etc/nginx/fastcgi.conf.default
  ...........


三、测试nginx 
  启动nginx  
    systemctl start nginx

  设置开机启动 
    systemctl enable nginx

查看nginx 启动状态
  systemctl status nginx

查看是否监听

 ss -tnl | grep 80 
  LISTEN    0      128          *:80                      *:*                  
  LISTEN    0      128        :::80                      :::* 


测试 nginx

在浏览器中输入 nginx 服务器的ip 地址 
  备注:如果不能正常访问,关闭防火墙
  systemctl stop firewalld.service #停止firewall
  systemctl disable firewalld.service #禁止firewall开机启动
  firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)

  测试页面

安装成功

四、nginx 的配置文件说明

配置文件路径  /etc/nginx/nginx.conf

  #运行用户
    user nginx;
  #启动进程,通常设置成和 cpu 的数量相等
    worker_processes 1;
  #全局错误日志及 PID 文件
    #error_log logs/error.log;
    #error_log logs/error.log notice;
    #error_log logs/error.log info;
    #pid logs/nginx.pid;
  #工作模式及连接数上限
    events {
      #epoll 是多路复用 IO(I/O Multiplexing)中的一种方式,
      #仅用于 linux2.6 以上内核,可以大大提高 nginx 的性能
      use epoll;
      #单个后台 worker process 进程的最大并发链接数
      worker_connections 1024;
      # 并发总数是 worker_processes 和 worker_connections 的乘积
      # 即 max_clients = worker_processes * worker_connections
      # 在设置了反向代理的情况下, max_clients = worker_processes * worker_connections / 4

      为什么
      # 为什么上面反向代理要除以 4,应该说是一个经验值
      # 根据以上条件,正常情况下的 Nginx Server 可以应付的最大连接数为: 4 * 8000 = 32000
      # worker_connections 值的设置跟物理内存大小有关
      # 因为并发受 IO 约束, max_clients 的值须小于系统可以打开的最大文件数
      # 而系统可以打开的最大文件数和内存大小成正比,一般 1GB 内存的机器上可以打开的文件数大约是10 万左右
      # 我们来看看 360M 内存的 VPS 可以打开的文件句柄数是多少:
      # $ cat /proc/sys/fs/file-max
      # 输出 34336
      # 32000 < 34336,即并发连接总数小于系统可以打开的文件句柄总数,这样就在操作系统可以承受的范围之内
      # 所以, worker_connections 的值需根据 worker_processes 进程数目和系统可以打开的最大文件总数进行适当地进行设置
      # 使得并发总数小于操作系统可以打开的最大文件数目
      # 其实质也就是根据主机的物理 CPU 和内存进行配置
      # 当然,理论上的并发总数可能会和实际有所偏差,因为主机还有其他的工作进程需要消耗系统资源。
      # ulimit -SHn 65535
    }
    http {
      #设定 mime 类型,类型由 mime.type 文件定义
      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 指令指定 nginx 是否调用 sendfile 函数( zero copy 方式)来输出文件,
      #对于普通应用,必须设为 on,
      #如果用来进行下载等应用磁盘 IO 重负载应用,可设置为 off,
      #以平衡磁盘与网络 I/O 处理速度,降低系统的 uptime.
      sendfile on;
      #tcp_nopush on;
      #连接超时时间
      #keepalive_timeout 0;
      keepalive_timeout 65;
      tcp_nodelay on;

      #开启 gzip 压缩
      gzip on;
      gzip_disable "MSIE [1-6].";
      #nginx 传输文件大小,默认为1M

      client_max_body_size 20m;
      client_header_buffer_size 32k;
      large_client_header_buffers 4 32k;

      #设定请求缓冲
      client_header_buffer_size 128k;
      large_client_header_buffers 4 128k;

     #设定虚拟主机配置
     server {
        #侦听 80 端口
        listen 80;
        #定义使用 www.linuxidc.com 访问
        server_name www.linuxidc.com;
        #定义服务器的默认网站根目录位置
        root html;
        #设定本虚拟主机的访问日志
        access_log logs/nginx.access.log main;
        #默认请求
        location / {
          #定义首页索引文件的名称
          index index.php index.html index.htm;
        }
        # 定义错误提示页面
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }

        #静态文件, nginx 自己处理
        location ~ ^/(images|javascript|js|css|flash|media|static)/ {
          #过期 30 天,静态文件不怎么更新,过期可以设大一点,
          #如果频繁更新,则可以设置得小一点。
          expires 30d;
        }
        #PHP 脚本请求全部转发到 FastCGI 处理. 使用 FastCGI 默认配置.

        location ~ .php$ {
          fastcgi_pass 127.0.0.1:9000;
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          include fastcgi_params;
        }

        #禁止访问 .htxxx 文件
        location ~ /.ht {
          deny all;
        }
      }
    }

原文地址:https://www.cnblogs.com/wdp1990/p/9546561.html