nginx基础

nginx 是一个应用比较广, 功能比较强大的web服务器.
从这里开始, 我们将初步了解一下nginx的安装 和基本的使用.
(以下内容基于ubuntu操作系统)
官方文档

安装

  • 安装openssl (nginx依赖)
    sudo apt-get install openssl
  • 从官网下载安装包
  • tar -zvxf nginx_***.tar.gz 解压
  • 进入解压后的目录, 执行 ./configure --prefix=[dest/dir] --with-http_ssl_module --with-http_stub_status_module
  • make && sudo make install 执行安装步骤

启动

  • 用非root用户启动Apache|Nginx的方法

    • 进入nginx可执行文件目录(默认是 /usr/local/nginx/sbin/nginx)
    • sudo chown root:nginx nginx
    • sudo chmod u+s nginx – 修改文件的SUID位, 使该文件在执行时有root权限
  • 启动命令
    nginx -c /usr/local/nginx/conf/nginx.conf

  • 重启
    修改了配置文件,需要重启的时候,有以下两种方法

    • 使用 nginx自带命令(推荐)
      nginx -s reload
    • 找到nginx进程, 使用kill 命令发送信号
      1
      2
      ps -es | grep nginx
      kill -HUP [主进程号]
  • 测试配置文件语法是否正确
    nginx -s /usr/local/nginx/conf/nginx.conf

  • 关闭

    1
    2
    3
    4
    5
    6
    7
    8
    #查询nginx主进程号
    ps -ef | grep nginx
    #从容停止
    kill -QUIT [主进程号]
    #快速停止
    kill -TERM [主进程号]
    #强制停止
    kill -KILL [主进程号]
  • 升级

    1
    2
    3
    4
    + #先用新程序替换旧程序文件
    + `kill -USR2 [旧版程序的主进程号]`
    #此时旧的nginx主进程会把自己的进程文件改名为.oldbin,然后执行新版nginx,此时新旧版本同时运行
    + kill -WINCH [旧版本主进程号]

配置

  • 修改环境变量, 在PATH变量的最后追加nginx的可执行文件目录

    1
    2
    export NGINX_HOME=/usr/local/nginx/sbin
    export PATH=$NGINX_HOME:$PATH
  • 配置的修改主要是修改 nginx.conf

    1. index 配置在 http 段, 这样可以避免 在每个server段写重复的index字段
    2. root 配置在 server段, 不用在每个 location 段 单独配置
      • 在conf目录下新建 conf.d 文件夹, 每个不同的站点, 配置不同的conf文件.
      • 在 nginx.conf http段最后添加 require conf.d/*.conf, 这样就可以把所有配置引用到
  • rewrite
    nginx rewrite 指令
    nginx rewrite 指令
    //todo 待完善

SSL相关

nginx配置SSL实现服务器/客户端双向认证

  • 生成密钥, 颁发证书

    1
    2
    3
    4
    5
    6
    7
    8
    #用于生成rsa私钥文件
    openssl genrsa -des3 -out server.key 1024
    #用于生成证书请求
    openssl req -new -key server.key -out server.csr
    #利用openssl进行RSA为公钥加密
    openssl rsa -in server.key -out server_nopwd.key
    #生成证书
    openssl x509 -req -days 365 -in server.csr -signkey server_nopwd.key -out server.crt
  • 添加nginx证书

    1
    2
    3
    4
    5
    6
    server {
    listen 443 ssl;
    #证书路径
    ssl_certificate /usr/local/nginx/ssl/server.crt;
    ssl_certificate_key /usr/local/nginx/ssl/server_nopwd.key;
    }

负载均衡

nginx 的负载均衡由 HTTP Upstream 实现, 可以使用 upstream 关键字来配置.
upstream 一共支持六种负载均衡算法:

* 轮询 (default)    每个服务器可以配置weight值, 根据weight 的数值,来决定使用该服务器的几率
* ip_hash           请求按ip地址hash到固定的服务器
* url_hash          请求按访问的url来hash到固定服务器
* fair              请求按响应时间分配服务器
* less_conn         使用当前连接最少的服务器
* hash              包含  普通hash 和 一致性 hash 两种

upstream 支持的服务器状态参数:

* down              该服务器不参与负载均衡
* backup            只有当其他非backup服务器都不可用,才启用该服务器
* max_fails         允许请求失败次数
* fail_timeout      到达最大失败次数后,停用时长

TIPS:当使用ip_hash算法时, 服务器不能是backup的

同一端口,不同站点

  • site1.conf:

    1
    2
    3
    4
    server {
    listen 80;
    server_name site1;
    }
  • site2.conf:

    1
    2
    3
    4
    server {
    listen 80;
    server_name site2;
    }
  • /etc/hosts

    1
    2
    127.0.0.1 site1
    127.0.0.1 site2

当用户访问site1时, 便会匹配到第一个配置, 使用site1.conf

php-fpm

配置方式

1
2
3
4
5
6
7
location ~ [^/]\.php(/|$) {
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
}

nginx 连接fastcgi有两种方式:

  • tcp/ip
    修改/usr/local/php/etc/php-fpm.conf文件.

    `listen=127.0.0.1:9000`
    

    修改 nginx.conf
    fastcgi_pass 127.0.0.1:9000;
    重启php-fpm, nginx

  • unix domain socket
    unit domain socket 用于同一个操作系统下多个进程间通信用.
    与internet socket 类似,只是它不是用网络底层协议通信.
    配置方式:
    修改/usr/local/php/etc/php-fpm.conf文件.

    1
    2
    3
    4
    listen = /tmp/php-cgi.sock
    listen.owner = service
    listen.group = service
    ;listen.mode = 0660

修改 nginx.conf
fastcgi_pass unix:/tmp/php-cgi.sock;
重启php-fpm, nginx


nginx基础
https://leafflow.top/2016/04/12/other/nginx基础-2016-04-12/
作者
vollen
发布于
2016年4月12日
许可协议