Nginx安装
docker-compose.yml
version: '3.1'
services:
nginx:
restart: always
image: daocloud.io/library/nginx:latest
container_name: nginx
ports:
- 80:80
volumes:
- /usr/local/docker/nginx/conf.d:/etc/nginx/conf.d
配置文件
nginx 目录下 nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
# 以上为全局块:
# worker_processes:数值越大,nginx的并发能力越强,这是根据服务器的配置编写
# error_log:数值越大,nginx的错误日志存放的位置
events {
worker_connections 1024;
}
# 以上为events块:
# worker_connections:数值越大,nginx的并发能力越强,这是根据服务器的配置编写
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;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
# 以上为http块:
# include代表引入一个外部的文件 -> /mine.types中放着大量的媒体类型
# include /etc/nginx/conf.d/*.conf -> 引入了conf.d目录下的以 .conf结尾的配置文件
nginx目录下conf.d目录下default.conf
server {
listen 80;
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;
# 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;
#}
}
# 以上为server块:
# listen:代表nginx监听的端口号
# localhost:代表nginx接受请求的ip
# localtion块:
# root:将接受到的请求根据/usr/share/nginx/html/ 去查找静态资源
# index:默认去上述的路径中找到index.html或者index.htm
Nginx反向代理
nginx目录下conf.d目录下default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
# 进入到nginx的话直接转发到这个地址
proxy_pass http://192.168.0.99:9003;
}
}
location的路径映射
# 1. = 匹配
location = / {
# 精准匹配,主机名后面不能带任何的字符串
}
# 2. 通用匹配
location /xxx {
# 匹配所有以/xxx开头的路径
}
# 3. 正则匹配
location ~ /xxx {
# 匹配所有以/xxx开头的路径
}
# 4. 匹配开头路径
location ^~ /xxx{
# 匹配所有以/xxx开头的路径
}
# 5. 匹配结尾路径
location ~* \. (git|jpg|png)${
# 匹配以gif或jpg或png结尾的路径
}
location匹配规则
语法规则
location [=|~|~*|^~] /uri/ { … }
模式 | 含义 |
---|---|
location = /uri | = 表示精确匹配,只有完全匹配上才能生效 |
location ^~ /uri | ^~ 开头对URL路径进行前缀匹配,并且在正则之前。 |
location ~ pattern | 开头表示区分大小写的正则匹配 |
location ~* pattern | 开头表示不区分大小写的正则匹配 |
location /uri | 不带任何修饰符,也表示前缀匹配,但是在正则匹配之后 |
location / | 通用匹配,任何未匹配到其它location的请求都会匹配到,相当于switch中的default |
前缀匹配时,Nginx 不对 url 做编码,因此请求为 /static/20%/aa
,可以被规则^~ /static/ /aa
匹配到(注意是空格)
多个 location 配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):
- 首先精确匹配 =
- 其次前缀匹配 ^~
- 其次是按文件中顺序的正则匹配
- 然后匹配不带任何修饰的前缀匹配。
- 最后是交给 / 通用匹配
当有匹配成功时候,停止匹配,按当前匹配规则处理请求
注意:前缀匹配,如果有包含关系时,按最大匹配原则进行匹配。比如在前缀匹配:location /dir01
与location /dir01/dir02
,如有请求http://localhost/dir01/dir02/file
将最终匹配到location /dir01/dir02
匹配成功之后不再往下匹配
例子,有如下匹配规则:
location = / {
echo "规则A";
}
location = /login {
echo "规则B";
}
location ^~ /static/ {
echo "规则C";
}
location ^~ /static/files {
echo "规则X";
}
location ~ \.(gif|jpg|png|js|css)$ {
echo "规则D";
}
location ~* \.png$ {
echo "规则E";
}
location /img {
echo "规则Y";
}
location / {
echo "规则F";
}
那么产生的效果如下:
- 访问根目录
/
,比如http://localhost/
将匹配规则A
- 访问
http://localhost/login
将匹配规则B
,http://localhost/register
则匹配规则F
- 访问
http://localhost/static/a.html
将匹配规则C
- 访问
http://localhost/static/files/a.exe
将匹配规则X
,虽然规则C
也能匹配到,但因为最大匹配* 原则,最终选中了规则X
。你可以测试下,去掉规则 X ,则当前 URL 会匹配上 规则C。 - 访问
http://localhost/a.gif, http://localhost/b.jpg
将匹配规则D
和规则 E
,但是规则 D
顺序* 优先,规则 E
不起作用,而http://localhost/static/c.png
则优先匹配到规则 C
- 访问
http://localhost/a.PNG
则匹配规则 E
,而不会匹配规则 D
,因为规则 E
不区分大小写。 - 访问
http://localhost/img/a.gif
会匹配上规则D
,虽然规则Y
也可以匹配上,但是因为正则匹配优先,而忽略了规则Y
。 - 访问
http://localhost/img/a.tiff
会匹配上规则Y
。 - 访问
http://localhost/category/id/1111
则最终匹配到规则 F
,因为以上规则都不匹配,这个时候应该是 Nginx 转发请求给后端应用服务器,比如 FastCGI(php),tomcat(jsp),Nginx 作为反向代理服务器存在。
所以实际使用中,笔者觉得至少有三个匹配规则定义,如下:
# 直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。
# 这里是直接转发给后端应用服务器了,也可以是一个静态首页
# 第一个必选规则
location = / {
proxy_pass http://tomcat:8080/index
}
# 第二个必选规则是处理静态文件请求,这是 nginx 作为 http 服务器的强项
# 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
root /webroot/res/;
}
# 第三个规则就是通用规则,用来转发动态请求到后端应用服务器
# 非静态文件请求就默认是动态请求,自己根据实际把握
# 毕竟目前的一些框架的流行,带.php、.jsp后缀的情况很少了
location / {
proxy_pass http://tomcat:8080/
}
rewrite 语法
- last – 基本上都用这个 Flag
- break – 中止 Rewirte,不再继续匹配
- redirect – 返回临时重定向的 HTTP 状态 302
- permanent – 返回永久重定向的 HTTP 状态 301
1、下面是可以用来判断的表达式:
-f 和 !-f 用来判断是否存在文件
-d 和 !-d 用来判断是否存在目录
-e 和 !-e 用来判断是否存在文件或目录
-x 和 !-x 用来判断文件是否可执行
2、下面是可以用作判断的全局变量
例:http://localhost:88/test1/test2/test.php?k=v
$host:localhost
$server_port:88
$request_uri:/test1/test2/test.php?k=v
$document_uri:/test1/test2/test.php
$document_root:D:\nginx/html
$request_filename:D:\nginx/html/test1/test2/test.php
redirect 语法
server {
listen 80;
server_name start.igrow.cn;
index index.html index.php;
root html;
if ($http_host !~ "^star\.igrow\.cn$") {
rewrite ^(.*) http://star.igrow.cn$1 redirect;
}
}
防盗链
location ~* \.(gif|jpg|swf)$ {
valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid_referer) {
rewrite ^/ http://$host/logo.png;
}
}
根据文件类型设置过期时间
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
expires 1h;
break;
}
}
禁止访问某个目录
location ~* \.(txt|doc)${
root /data/www/wwwroot/linuxtone/test;
deny all;
}
完整demo
upstream appzuul {
server 192.168.*.*:18600;
server 192.168.*.*:18600;
}
server {
listen 80;
server_name a.cooper.com b.cooper.com;
rewrite ^/(.*)$ https://a.cooper.com/$1 permanent;
}
server {
listen 443 ssl;
server_name a.cooper.com b.cooper.com;
root /opt/web_html/auth.100credit.com;
index login index.html index.htm login.html applicationMasterPage.html;
ssl_certificate /opt/nginx-1.4.7/conf/ssl/100credit.com.crt;
ssl_certificate_key /opt/nginx-1.4.7/conf/ssl/100credit.com.key;
ssl_prefer_server_ciphers on;
ssl_protocols SSLv3 TLSv1;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
access_log logs/a.cooper.com_access.log;
error_log logs/a.cooper.com_error.log;
error_page 404 /view/404.html;
#重定向首页
location ~ /base/{
rewrite ^/base/login\.html /#/login permanent;
}
#前端目录调整
location ^~ /v/
{
rewrite ^/v/(.*)$ /$1 permanent;
}
# 静态资源配置方式一
location / {
root /opt/web_html/apiservice.100credit.com;
index index.html;
try_files $uri $uri/ /index.html;
}
# 静态资源配置方式二
# location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|js|css|json|md|woff)$
# {
# root /opt/web_html/auth.100credit.com;
# index index.html index.htm applicationMasterPage.html;
# }
location ^~ /api/
{
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://appzuul;
}
location ^~ /zuul/api/
{
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://appzuul;
}
}
Nginx负载均衡
负载均衡策略
-
轮询:
将客户端发起的请求,平均的分配给每一台服务器。
-
权重:
会将客户端的请求,根据服务器的权重值不同,分配不同的数量。
-
ip_hash:
基于发起请求的客户端的ip地址不同,他始终会将请求发送到指定的服务器上。
轮询
default.conf
upstream my-server{
server 192.168.199.109:8080;
server 192.168.199.109:8081;
}
server{
listen 80;
server_name localhost;
location / {
proxy_pass http://my-server/;
}
}
权重
default.conf
upstream my-server{
server 192.168.199.109:8080 weight=10;
server 192.168.199.109:8081 weight=5;
}
server{
listen 80;
server_name localhost;
location / {
proxy_pass http://my-server/;
}
}
ip_hash
default.conf
upstream my-server{
ip_hash;
server 192.168.199.109:8080;
server 192.168.199.109:8081;
}
server{
listen 80;
server_name localhost;
location / {
proxy_pass http://my-server/;
}
}
Nginx动静分离
nginx的并发能力公式:
worker_processes*worker_connections / 4|2 = Nignx最终的并发能力
动态资源需要 /4,静态资源需要 /2
Nginx通过动静分离,来提升Nginx的并发能力,更快的给用户响应
动态资源代理
# 配置如下
location / {
proxy_pass 路径;
}
静态资源代理
# 配置如下
location / {
root 静态资源路径;
index 默认访问路径下的什么资源;
autoindex on; # 代表展示静态资源全的全部内容,以列表的形式展开。
}