Nginx入门指南

2019/01/12 posted in  部署
Tags:  #nginx

简介

  • 轻量级HTTP服务器
  • 事件驱动,异步非阻塞
  • 反向代理与负载均衡
  • 高并发,低内存占用

安装Nginx

# Ubuntu
apt-get install nginx
# Centos
yum install nginx

常用命令

  • 启动
nginx
  • 停止
# 立即停止
nginx -s stop
# 从容停止
nginx -s quit
# 杀死进程
killall nginx
  • 重新载入配置
nginx -s reload

配置

cd /etc/nginx
vim nginx.conf
  • nginx配置文件
#运行用户,默认即是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        on;   #开启高效传输模式
    #tcp_nopush     on;    #减少网络报文段的数量
    keepalive_timeout  65;  #保持连接的时间,也叫超时时间
    #gzip  on;  #开启gzip压缩
    include /etc/nginx/conf.d/*.conf; #包含的子配置项位置和文件
}
  • 子配置conf.d/xxx.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;
    #}
}

访问控制

  • IP
 location / {
        deny   123.9.51.42; # 禁止访问的ip
        allow  45.76.202.231; # 允许访问的ip
    }
  • 路由
# =号代表精确匹配,使用了=后是根据其后的模式进行精确匹配
location =/img{
    allow all;
}
location =/admin{
    deny all;
}
  • 正则
 location ~\.php$ {
    deny all;
}

虚拟主机

配置虚拟主机可以基于端口号、基于IP和基于域名。

  • 端口号
server{
        listen 8001;
        server_name localhost;
        root /usr/share/nginx/html/html8001;
        index index.html;
}
  • IP
server{
        listen 80;
        server_name 112.74.164.244;
        root /usr/share/nginx/html/html8001;
        index index.html;
}
  • 域名
server{
        listen 80;
        server_name nginx2.jspang.com;
        location / {
                root /usr/share/nginx/html/html8001;
                index index.html index.htm;
        }
}

反向代理

server{
        listen 80;
        server_name nginx2.jspang.com;
        location / {
               proxy_pass http://jspang.com;
        }
}

其他配置:

  • proxy_set_header :在将客户端请求发送给后端服务器之前,更改来自客户端的请求头信息。

  • proxy_connect_timeout:配置Nginx与后端代理服务器尝试建立连接的超时时间。

  • proxy_read_timeout : 配置Nginx向后端服务器组发出read请求后,等待相应的超时时间。

  • proxy_send_timeout:配置Nginx向后端服务器组发出write请求后,等待相应的超时时间。

  • proxy_redirect :用于修改后端服务器返回的响应头中的Location和Refresh。

适配PC或移动设备

Nginx通过内置变量$http_user_agent,可以获取到请求客户端的userAgent,就可以用户目前处于移动端还是PC端,进而展示不同的页面给用户。

server{
     listen 80;
     server_name nginx2.jspang.com;
     location / {
      root /usr/share/nginx/pc;
      if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
         root /usr/share/nginx/mobile;
      }
      index index.html;
     }
}

Gzip

http {
   .....
    gzip on;
    gzip_types text/plain application/javascript text/css;
   .....
}

Nginx提供了专门的gzip模块,并且模块中的指令非常丰富。

  • gzip : 该指令用于开启或 关闭gzip模块。
  • gzip_buffers : 设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。
  • gzip_comp_level : gzip压缩比,压缩级别是1-9,1的压缩级别最低,9的压缩级别最高。压缩级别越高压缩率越大,压缩时间越长。
  • gzip_disable : 可以通过该指令对一些特定的User-Agent不使用压缩功能。
  • gzip_min_length:设置允许压缩的页面最小字节数,页面字节数从相应消息头的Content-length中进行获取。
  • gzip_http_version:识别HTTP协议版本,其值可以是1.1.或1.0.
  • gzip_proxied : 用于设置启用或禁用从代理服务器上收到相应内容gzip压缩。
  • gzip_vary : 用于在响应消息头中添加Vary:Accept-Encoding,使代理服务器根据请求头中的Accept-Encoding识别是否启用gzip压缩。

« GIT备忘手册 git clone、git pull和git fetch的用法及区别 »