Nginx服务器安装及配置文件详解

Nginx

Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。Nginx百科

编译安装Nginx:

下载nginx软件包:Nginx下载地址

#解压:
[ root@localhost ~]#  tar xf  nginx-1.6.2.tar.gz
#安装开发包组和一些必装的包:
[ root@localhost ~]# yum -y groupinstall  "development tools"   | yum -y install pcre-devel   | yum -y install openssl-devel 

#编译安装
[ root@localhost ~]# cd nginx-1.6.2
[ root@localhost ~]# 
./configure  \
--prefix=/usr/local/nginx \
--conf-path=/etc/nginx/nginx.conf  \
--user=nginx \
--group=nginx \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock  \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_flv_module \
--with-http_mp4_module \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--with-debug \
#解释:
 --prefix=/etc/nginx 
#安装路径,指明nginx程序文件安装路径
--conf-path=/etc/nginx/nginx.conf 
#主配置文件安装位置
--error-log-path=/var/log/nginx/error.log 
#错误日志文件安装位置
 --http-log-path=/var/log/nginx/access.log 
#访问日志文件安装位置
--pid-path=/var/run/nginx.pid
#指明pid文件安装位置
--lock-path=/var/run/nginx.lock 
#锁文件安装位置
--http-client-body-temp- path=/var/cache/nginx/client_temp 
#客户端body部分的临时 文件存放路径,如果服务器允许客户端使用put方法提交大数据 时,临时存放的磁盘路径
 --http-proxy-temp-path=/var/cache/nginx/proxy_temp 
#作为代理服务器,服务器响应报文的临时文件存放路径
--http-fastcgi-temp- path=/var/cache/nginx/fastcgi_temp 
#作为fastcgi代理服务 器,服务器响应报文的临时文件存放路径
 --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp 
#作为uwsgi代理服务器,服务器响应报文的临时文件存放路径
#作为 scgi反代服务器,服务器响应报文的临时文件存放路径
--user=nginx 
#指明以那个身份运行worker进程,主控master 进程一般由root运行
--group=nginx
--with-http_ssl_module 
#ssl加密协议模块

[ root@localhost ~]#  make && make install
#开始编译安装     

[ root@localhost ~]# nginx 
#启动服务
开始配置Nginx
[ root@localhost ~]#  groupadd -r nginx
#创建nginx组
[ root@localhost ~]# useradd -g nginx -r nginx
#创建nginx账号并指明组

 [ root@localhost ~]#
 mkdir -pv /var/tmp/nginx/{client,proxy,fastcgi,uwsgi}
 #创建目录(之前编译时写了很多模块指定了目录,这里需要创建)

 [ root@localhost ~]# vim /etc/nginx/nginx.conf 
 #nginx主配置文件
#------------------------------------------------------------
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
#以身份运行nginx
worker_processes auto;
#worker进程的数量;通常应该为当前主机的cpu的物理核心数 
error_log /var/log/nginx/error.log;
#错误日志路径
pid /var/run/nginx.pid;
#进程id文件
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
#模块配置文件路径,写在主配置文件中就可以调用了

events {
    worker_connections  1024;
    #每个worker进程所能打开的最大并发连接数数量
}


http {
    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;
    #定义日志格式,并且使用

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
}
#-------------------------------------------------------------
设置一个虚拟主机:
[ root@localhost ~]#  vim /etc/nginx/conf.d/vhosts.conf
server {
        listen 80;
        #监听端口
        server_name www.yy.com
        #指定主机名网站
        root /web;
        #网站文件存放目录
}
#配置好后,重置配置文件:nginx -s reload
#检查语法有没有错误:nginx -t 
#提示:主机名可以写正则表达式
虚拟主机的主机名称后可跟多个由空白字符分隔的字符串,支持*通配任意长度的任意字符
server_name *.yyc.com www.yyc.*
                                                                          支持~起始的字符做正则表达式模式匹配,性能原因慎用
                                                                        server_name ~^www\d+\.yyc\.com$ \d 表示 [0-9]
                                                                          匹配优先级机制从高到低:精确匹配有限极高                                      (1) 首先是字符串精确匹配 如:www.yyc.com                                        (2) 左侧*通配符 如:*.yyc.com                                          (3) 右侧*通配符 如:www.yyc.*                                              (4) 正则表达式 如: ~^.*\.yyc\.com$                                              (5) default_server

启用压缩功能
Syntax: gzip on | off;
Default:    
gzip off;
Context:    http, server, location, if in location
#Context:指的是能在哪些里面写

[ root@localhost ~]#  vim /etc/nginx/vhosts.conf
   server {
        listen 80;
        root /web/www1/ ;
        gzip on;
        #是否开启压缩
        gzip_comp_level 9;
        #压缩级别
        gzip_min_length 128;    
        #启用压缩功能的响应报文大小阈值
        gzip_vary on | off
        #如果启用压缩,是否在响应报文首部插入“Vary: Accept- Encoding”
        gzip_types  application/javascript  text/css text/plain;
#指明仅对哪些类型的资源执行压缩操作;即压缩过滤器 默认包含有text/html,不用显示指定,否则出错


}
设置ssl加密
Syntax: ssl on | off;
Default:    
ssl off;
Context:    http, server

[ root@localhost ~]# mkdir /etc/nginx/ssl 
#创建存放证书目录
[ root@localhost ~]# cd /etc/pki/tls/certs 
[ root@localhost ~]# make nginx.crt
#创建证书文件
#CN  henan  zhengzhou  yyc   Opt www.yyc.com ,依次填入以上几个,这样创建出来的会被加密,我们还得解密
[ root@localhost ~]# openssl rsa -in nginx.key -out nginx2.key
[ root@localhost ~]# cp  nginx.crt nginx2.key /etc/nginx/ssl/
#把创建出来的证书文件放到刚刚创建存放私钥文件的目录即可

[ root@localhost ~]#  vim /etc/nginx/vhosts.conf
    server {
        liten 443 ssl
        root "/web/";
        ssl on;
    ssl_certificate /etc/nginx/ssl/nginx.crt; 
    #当前虚拟主机使用PEM格式的证书文件
    ssl_certificate_key /etc/nginx/ssl/nginx.key; 
    #当前虚拟主机上与其证书匹配的私钥文件
    ssl_session_cache shared:sslcache:20m; 
    ssl_session_timeout 10m;
proxy_pass反向代理
[ root@localhost ~]# vim /etc/nginx/conf.d/vhosts.conf
server {
        listen 80;
        server_name www.yyc.com
        root /web;
}
location / {
    proxy_pass http://www.yy.com
    #当访问www.yyc.com时直接跳转到www.yy.com
}
定义Nginx缓存
[ root@localhost ~]# vim /etc/nginx/nginx.conf
http {
proxy_cache_path /cache/nginx/  levels=1:1 keys_zone=mycache:32m;
                                }
[ root@localhost ~]#  mkdir - /cache/nginx 
#创建缓存目录
[ root@localhost ~]# chown -R nginx /cache/nginx 
#设置缓存目录权限

[ root@localhost ~]# vim /etc/nginx/conf.d/vhosts
    location / {
        proxy_cache yyc;
        proxy_pass http://172.18.33.33;
        proxy_cache_valid 200 1h;
    }
 #缓存的是别的机器的页面,这样一来当用户请求再来访问时,直接把缓存的页面给用户查看,就不用跑到真实机器上。
Nginx设置fastcgi(php-fpm)模式
[ root@localhost ~]# yum -y install php-fpm 
[ root@localhost ~]# vim /etc/php-fpm.d/www.conf
listen = 9000
#监听端口为9000 
#listen.allowed_clients = 127.0.0.1
#允许那些网站访问,注释掉表示是所有
[ root@localhost ~]#  service php-fpm start
#启动php-fpm,默认监听为9000端口
[ root@localhost ~]# mkdir  /web/php  #存放php文件目录,托管在这个目录,别的目录放了不起作用的
[ root@localhost ~]# vim /etc/nginc/conf.d/vhosts.conf
  location ~ \.php$ {
        fastcgi_pass 172.18.33.33:9000;
        #指明fastcgi地址
        fastcgi_index index.php ;
        #指明默认页面文件
        fastcgi_param SCRIPT_FILENAME /web/php/$fastcgi_script_name;
        include fastcgi_params;
}
Nginx设置轮训调度和权重调度
[ root@localhost ~]# vim /etc/nginx/nginx.conf
upstream dd {
            server 172.18.33.11  weight1 ; #权重高则优先调度
            server 172.18.33.22  ;
            server 172.18.33.33 backup; 
#当上面2台机器挂了之后就会启动这个33机器的页面,我设置的是sorry,如果写的是down,服务器就不会调度这台机器了,
ip_hash;  
#当一台主机访问了服务器后,服务器会会给调度到另外一台主机并且记录ip的hash值,当这台机器再次访问时还是继续调度到第一次访问的机器,一直调度到第一次访问的机器
hash $request ; 
#当一台主机访问后不同的文件会调度到不同的服务器上(前提服务器有都有相同的文件),服务器会记录这台机器访问的url(文件或图片)的hash,当再次访问同一个文件的时候,会显示第一次调度的机器上,相当于有了缓存一样
hash $request consistent; #这种效果更好,原理不明白

#注意:下面几个hash写一个即可。
[ root@localhost ~]# vim  /etc/nginx/conf.d/vhosts.conf
    location / {
        proxy_pass http://dd ;
  } 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值