Nginx的安装与配置

Nginx是一款提供http、反向代理以及IMAP/POP3/SMTP的WEB服务器,它有着开源、免费、高性能及高并发等特色,近几年陆续超过微软的IIS和Apache成为了市场占比最高的WEB服务器javascript

数据来自 https://news.netcraft.com
web1.PNG
web2.PNG



这里简单记录下CentOS7.8环境下安装nginx的步骤与经常使用配置:php

1、安装

官网 http://nginx.org/en/download....css

  1. 安装依赖包html

    [root@bluse soft]# yum install -y pcre pcre-devel openssl  openssl-devel gcc gcc-c++ ncurses-devel perl
  2. 下载nginx源码包并解压java

    [root@bluse soft]# wget http://nginx.org/download/nginx-1.10.3.tar.gz
    [root@bluse soft]# tar zxvf nginx-1.10.3.tar.gz
  3. 编译安装node

    [root@bluse soft]# groupadd nginx
    [root@bluse soft]# useradd nginx -g nginx -s /sbin/nologin -M
    [root@bluse soft]# cd nginx-1.10.3
    [root@bluse nginx-1.10.3]#./configure --prefix=/data/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
    [root@bluse nginx]#make && make install
  4. 安装完成后能够确认下安装目录和版本linux

    [root@bluse nginx]# ls
    conf  html  logs  sbin
    [root@bluse nginx]# sbin/nginx -v
    nginx version: nginx/1.10.3
  5. 启动脚本
    vim /usr/lib/systemd/system/nginx.servicenginx

    [Unit]
    Description=nginx 
    After=network.target 
    
    [Service] 
    Type=forking 
    ExecStart=/data/nginx/sbin/nginx
    ExecReload=/data/nginx/sbin/nginx reload
    ExecStop=/data/nginx/sbin/nginx quit
    PrivateTmp=true 
    
    [Install] 
    WantedBy=multi-user.target

    systemctl daemon-reload
    systemctl enable nginxc++

2、经常使用配置
  • nginx.conf
#运行用户
user nginx;
 
#启动进程, 一般设置成和cpu的数量相等; 用cat /proc/cpuinfo查看cpu信息, cpu cores一栏显示内核数
worker_processes  1;
 
#全局错误日志及PID文件(/data/logs/nginx/logs/error.log)
error_log  /data/logs/nginx/error.log;
#进程id存储文件(/var/run/nginx.pid)
pid           /var/run/nginx.pid;
#工做模式及链接数上限
events {
    use   epoll;                            #epoll是多路复用IO(I/O Multiplexing)中的一种方式,可是仅用于linux2.6以上内核,能够大大提升nginx的性能
    worker_connections  1024;     #单个后台worker process进程的最大并发连接数
    # multi_accept on;                #在nginx得到有新链接的通知以后,接受尽量多的链接, 若是worker_connections设置过低的话,这样可能会形成拥堵
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
    #设定mime类型,类型由mime.type文件定义,能够打开mime.type来看(/usr/local/nginx/conf/mime.types)
    #MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型.
    include       /data/nginx/mime.types;
    #默认文件类型以二进制数据传输
    default_type  application/octet-stream;
    #设定日志文件(/data/logs/nginx/access.log)
    access_log    /data/logs/nginx/access.log;
   
    #关闭在错误页面中的nginx版本数字,这样对于安全性是有好处的。
    server_tokens off;
 
    #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
    #必须设为 on,若是用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,下降系统的uptime.
    sendfile        on;
   
    #告诉nginx在一个数据包里发送全部头文件,而不一个接一个的发送
    #tcp_nopush     on;
 
    #给客户端分配keep-alive链接超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #告诉nginx不要缓存数据,而是一段一段的发送;当须要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能当即获得返回值
    tcp_nodelay        on;
    
    #开启gzip压缩
    gzip  on;
    #指定的客户端禁用gzip功能。咱们设置成IE6或者更低版本以使咱们的方案可以普遍兼容。
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
 
    #设定请求缓冲
 、、   client_header_buffer_size    1k;
   、、 large_client_header_buffers  4 4k;
 
    open_file_cache max=100000 inactive=20s;     #打开缓存的同时也指定了缓存最大数目,以及缓存的时间; 咱们能够设置一个相对高的最大时间,这样咱们能够在它们不活动超过20秒后清除掉。
    open_file_cace_valid 30s;                                #在open_file_cache中指定检测正确信息的间隔时间
    open_file_cache_min_uses 2;                           #定义了open_file_cache中指令参数不活动时间期间里最小的文件数
    open_file_cache_errors on;                              #指定了当搜索一个文件时是否缓存错误信息,也包括再次给配置中添加文件。咱们也包括了服务器模块,这些是在不一样文件中定义的。若是你的服务器模块不在这些位置,你就得修改这一行来指定正确的位置。
 
 
    #虚拟主机的配置文件
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
   
    #设定负载均衡的服务器列表
    upstream mysvr {
         #weigth参数表示权值,权值越高被分配到的概率越大
         #本机上的Squid开启3128端口
         server 192.168.8.1:3128 weight=5;
         server 192.168.8.2:80  weight=1;
         server 192.168.8.3:80  weight=6;
    }
 
    server {
        #侦听80端口
        listen       80;
        #定义使用www.xx.com访问
        server_name  www.xx.com;
        #设定本虚拟主机的访问日志
        access_log  logs/www.xx.com.access.log  main;
       
        #默认请求
        location / {
            root   /root;      #定义服务器的默认网站根目录位置
            index index.php index.html index.htm;   #定义首页索引文件的名称
            fastcgi_pass  www.xx.com;
            fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name; 
            include /data/nginx/fastcgi_params;
        }
       
        # 定义错误提示页面
        error_page   500 502 503 504 /50x.html;  
             location = /50x.html {
             root   /root;
        }
 
        #静态文件,nginx本身处理
        location ~ ^/(images|javascript|js|css|flash|media|static)/ {
             root /var/www/virtual/htdocs;
             #过时30天,静态文件不怎么更新,过时能够设大一点,若是频繁更新,则能够设置得小一点。
             expires 30d;
        }
 
        #PHP 脚本请求所有转发到 FastCGI处理. 使用FastCGI默认配置.
        location ~ \.php$ {
             root /root;
             fastcgi_pass 127.0.0.1:9000;
             fastcgi_index index.php;
             fastcgi_param SCRIPT_FILENAME /data/www/www$fastcgi_script_name;
             include fastcgi_params;
        }
 
        #设定查看Nginx状态的地址
        location /NginxStatus {
             stub_status            on;
             access_log              on;
             auth_basic              "NginxStatus";
             auth_basic_user_file  conf/htpasswd;
        }
 
        #禁止访问 .htxxx 文件
        location ~ /\.ht {
             deny all;
        }
     
     }
}
" 手动配置基于域名的虚拟主机 "
步骤: 
1. 在nginx.conf中的http段中, 一个server段就表示一个服务主机, 每添加一个虚拟主机, 增长一个server段便可; 
2. 若是单独新建目录存放虚拟主机配置文件, 只须要在http段中最后include进来, 如: include vhost/dog.farwish.conf; 
3. 这个dog.farwish.conf中只有server段的配置内容, 如:
 
 
server {
    listen       80;
    server_name  dog.farwish.com;
 
    location / {
        index  index.html index.htm index.php;
        root   /home/www/dog;
        if (!-e $request_filename){   
            rewrite ^(.*)$ /index.php/?s=$1 last; # TP rewrite模式
            break;
        }
 
        autoindex on;
    }   
 
    location ~ \.php {
        root           /data/www/dog;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }   
}
# 以上配置是在nginx1.6.2源码包配置文件的基础上改动.
  • 跳转httpsweb

    server {
     listen 80;
     server_name xx.xx.com;
     rewrite ^(.*) https://$server_name$1 permanent;
     }
    
    server {
     listen 443 ssl;
     server_name xx.xx.com;
     index  index.php index.html index.htm;
     ssl_certificate   cert/server.crt;
     ssl_certificate_key  cert/server.key;
     ssl_session_timeout 5m;
     ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
     ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
     ssl_prefer_server_ciphers on;
     client_max_body_size 100m;
     location / {
       proxy_pass http://172.16.60.170:9000;
     }
    }