# 用了nginx for win很久,安装也是超级简单。
# 还是用一下linux版的吧。环境是centos 6.5 x64

# 安装开始:
# 先安装依赖
yum install gcc-c++ 
yum -y install pcre* 
yum -y install openssl*

# 下载,可以wget 目前最新1.15.3
cd /opt
wget http://nginx.org/download/nginx-1.12.2.tar.gz

tar zxf nginx-1.12.2.tar.gz 
cd nginx-1.12.2

# 指定安装目录 、编译安装
./configure --prefix=/opt/nginx
make && make install

# 检查测试
/opt/nginx/sbin/nginx -t

 

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

# 启动  停止  退出 

/opt/nginx/sbin/nginx 
/opt/nginx/sbin/nginx -s stop
/opt/nginx/sbin/nginx -s quit
# 如果是centos7以上,已经注册为系统服务的:
systemctl stop nginx
systemctl start nginx
#---------------- 官方文档: -s 参数------------
# stop — fast shutdown
# quit — graceful shutdown
# reload — reloading the configuration file
# reopen — reopening the log files
#----------------------------------------------

 

# 查看进程,端口  检查运行情况

ps aux |grep nginx # master worker 至少各一个
netstat -tulnp | grep :80

# 如果想要命令方便执行,可将路径加入PATH变量中
vim /etc/profile # 加入 2 行

export NGINX_HOME=/opt/nginx
export PATH=$NGINX_HOME/sbin:$PATH

source /etc/profile # 生效

 

#-------------------- 配置文件 nginx.conf ---------------------------

以下版本有一些简单的优化, 注意那些写有注释的行。

user nginx;
worker_processes auto; # 进程数,一般设置为CPU核数,或者 auto 
pid /var/run/nginx.pid; # 记录进程PID文件,可以不启用

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
use epoll; # 使用高效的 epoll 方式
worker_connections 65535; # 单个worker进程同时打开的最大连接数 跟系统的 ulimit -n 有关
}

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;
server_tokens off; # 隐藏web出错时的版本号

 

 

虚拟主机 

超简单配置 基于不同域名的虚拟主机,其实就是:根据访问的不同网址对应不同的nginx中的不同文件夹。

先备份一个conf/nginx.conf文件,然后修改 http{}中的server, 删除原有的,示例如下:

## 注意:下面仅仅是http{}部分,不是nginx.conf的全部
http {
    server_tokens  on; 
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server { # 创建服务
        listen       80;
        server_name  wwww.node-6.com;  # 域名
        ## location 匹配URL路径地址,/ 表示匹配所有路径  =开头表示精确匹配  ~区分大小写  ~*不区分大小写(默认不区分)
        location / {  
            root   data/www;  # 根目录对应真实文件夹
            index  index.html index.htm index.jsp;
        }
    }
    
    server {
        listen       80;
        server_name  bbs.node-6.com;
        location / {
            root   data/bbs;
            index  index.html index.htm index.jsp;
        }
    }

}

对应地,需要在nginx下建立目录 data/www 和 data/bbs 以及各自目录下的文件。

执行 nginx -s reload 重新载入配置文件。 配置好DNS解析或hosts文件,浏览器测试访问。

 

超简单配置 基于不同端口号的虚拟主机,其实就是:根据访问网址的不同端口 对应不同的nginx中的不同文件夹。

和上面的配置差不多,只不过域名相同,listen不同:

    server { # 服务
        listen       80;
        server_name  wwww.node-6.com;  # 域名
        location / { 
            root   data/www;  # 根目录对应真实文件夹
            index  index.html index.htm index.jsp;
        }
    }    
    server {
        listen       81;
        server_name  wwww.node-6.com;
        location / {
            root   data/bbs;
            index  index.html index.htm index.jsp;
        }
    }
    server {
        listen       82;
        server_name  wwww.node-6.com;
        location / {
            root   data/img;
            index  index.html index.htm index.jsp;
        }
    }

同样,nginx -s reload 即可生效。

 

配置 nginx 反向代理

和上面的虚拟主机配置差不多,既可以不同端口,也可以不同域名,关键词 proxy_pass  示例如下:

## 不同端口的反向代理 server{}部分

    server { # 服务
        listen       80;
        server_name  wwww.node-6.com;  # 域名
        location / { 
            proxy_pass  http://127.0.0.1:8080;  # 反向代理本地 tomcat
            index  index.html index.htm index.jsp;
        }
    }    
    server {
        listen       81;
        server_name  www.node-6.com;  # 域名
        location / {
            proxy_pass  http://127.0.0.1:8081;  # 反向代理
            index  index.html index.htm index.jsp;
        }
    }
## 不同域名的反向代理 server{}部分

    server { # 服务
        listen       80;
        server_name  wwww.node-6.com;  # 域名
        location / { 
            proxy_pass  http://127.0.0.1:8080;  # 反向代理本地 tomcat
            index  index.html index.htm index.jsp;
        }
    }    
    server {
        listen       80;
        server_name  bbs.node-6.com;  # 域名
        location / {
            proxy_pass  http://127.0.0.1:8081;  # 本地另一个 tomcat
            index  index.html index.htm index.jsp;
        }
    }

配置完成后,同样,nginx -s reload 即可生效。

 

不同Location 做反向代理,示例如下:

## 不同Location的反向代理  server{}部分
## 注意目标地址末尾要有/

    server { # 服务
        listen       80;
        server_name  www.node-6.com;  # 域名
        location /www {  # 末尾有无/不影响
            proxy_pass  http://127.0.0.1:8080/;  # 末尾一定要/
            index  index.html index.htm index.jsp;
        }
        location /bbs {
            proxy_pass  http://127.0.0.1:8081/; 
            index  index.html index.htm index.jsp;
        }
    }

 

负载均衡:

使用 upstream 方式,配置也很简单:在server{} 上面定义一个 upstream backServer 然后在proxy_pass中指向backServer  示例如下:

## 以下部分全部应在 http{}内:
## 定义多个上游服务器(真实业务)服务器的IP和端口,默认采用轮询机制
    upstream backServer{
        server 127.0.0.1:8080;
        server 192.168.112.5:8080;
    }

    server {
        listen       80;
        server_name  www.node-6.com;  # 域名
        location / {
            proxy_pass  http://backServer/;  # 末尾一定要/
            index  index.html index.htm index.jsp;
        }
    }

负载均衡的方式:

轮询机制:轮流访问,非常均匀

权重机制:使用weight配置比例

ip hash: nginx获取IP地址hash运算固定分配到某服务器上,可以解决session共享问题

fair :第三方

url绑定:第三方 

 

权重机制的设置和上面的简单设置差不多,只在目标后面加了weight。示例如下:

    upstream backServer{
        server 127.0.0.1:8080 weight=1;
        server 192.168.112.5:8080 weight=3;
    }

 

IP绑定方式,只是多了行 ip_hash; 

upstream backServer{
        server 127.0.0.1:8080;
        server 192.168.112.5:8080;
        ip_hash;
    }

 

一般情况下,可能实际生产环境,配置轮询机制或者权重机制比较多见。

如果上游服务器有个突然挂了怎么办? 

所以,要设置好nginx的故障转移,示例如下:

## http{}中的两段配置
upstream backServer{ server
127.0.0.1:8080; server 127.0.0.1:8081; server 192.168.112.5:8080; } server { listen 80; server_name www.node-6.com; # 域名 location / { proxy_pass http://backServer/; # 末尾一定要/ ## 故障转移:设置超时时间 proxy_connect_timeout 1s; proxy_send_timeout 1s; proxy_read_timeout 1s; index index.html index.htm index.jsp; } }

 

URL重写:

参考:http://www.cnblogs.com/czlun/articles/7010604.html

    https://www.linuxidc.com/Linux/2014-01/95493.htm

 rewrite    <regex>    <replacement>    [flag];

关键字      正则        替代内容          flag标记

 

    。关键字:其中关键字error_log不能改变

    。正则:perl兼容正则表达式语句进行规则匹配

    。替代内容:将正则匹配的内容替换成replacement

    。flag标记:rewrite支持的flag标记

 

flag标记说明:

last  #本条规则匹配完成后,继续向下匹配新的location URI规则

break  #本条规则匹配完成即终止,不再匹配后面的任何规则

redirect  #返回302临时重定向,浏览器地址会显示跳转后的URL地址

permanent  #返回301永久重定向,浏览器地址栏会显示跳转后的URL地址

 

rewrite参数的标签段位置:

server, location, if

 

 实际配置文件示例: nginx/default.conf

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄