1.nginx下载安装

下载地址:http://nginx.org/en/download.html
解压:tar -xf nginx-1.21.6.tar.gz
安装依赖:
yum install -y gcc
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
安装:./configure --prefix=/usr/local/nginx
make
make install
到此安装完成
注意:要关闭防火墙
systemctl stop firewalld.service
systemctl disable --now firewalld

nginx从入门到入坟-LMLPHP

2.启动命令

cd /usr/local/nginx/sbin
[root@sg-15 sbin]# pwd
/usr/local/nginx/sbin
[root@sg-15 sbin]# ./nginx

访问:浏览器输入192.168.0.215

./nginx  启动
./nginx -s stop 快速停止
./nginx -s quit 优雅关闭,在退出前完成已经接受的连接请求
./nginx -s reload 重新加载配置,修改配置文件之后使用

nginx从入门到入坟-LMLPHP

3.服务脚本

vi /usr/lib/systemd/system/nginx.service

[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
//重新加载系统服务
systemctl daemon-reload
//开机启动nginx
systemctl enable nginx.service
//脚本启动
systemctl start nginx //启动
systemctl stop nginx //关闭
systemctl status nginx //状态
systemctl restart nginx //重启
systemctl reload nginx //重新加载配置文件

4.nginx目录说明

nginx从入门到入坟-LMLPHP

drwx------. 2 nobody root    6 4月  19 09:22 client_body_temp
drwxr-xr-x. 2 root   root 4096 4月  19 09:20 conf
drwx------. 2 nobody root    6 4月  19 09:22 fastcgi_temp
drwxr-xr-x. 2 root   root   40 4月  19 09:20 html
drwxr-xr-x. 2 root   root   58 4月  19 10:16 logs
drwx------. 2 nobody root    6 4月  19 09:22 proxy_temp
drwxr-xr-x. 2 root   root   19 4月  19 09:20 sbin
drwx------. 2 nobody root    6 4月  19 09:22 scgi_temp
drwx------. 2 nobody root    6 4月  19 09:22 uwsgi_temp

-------------------------------
conf:nginx配置文件目录
conf/nginx.conf:主配置文件
sbin:nginx主程序文件目录,启动需要用到
html:默认网页和静态资源
logs:日志目录
logs/access.log:记录访问日志
logs/error.log:记录访问出错日志(404等)
logs/nginx.pid:nginx启动的进程号

5.nginx配置文件

cat /usr/local/nginx/conf/nginx.conf
#user  nobody; #定义Nginx运行的用户和用户组
worker_processes  1; # 启动进程数量,对应cpu数量最佳

#error_log  logs/error.log;  #全局错误日志定义类型
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid; #进程pid文件

#事件驱动
events {
    worker_connections  1024; # 每一个进程可以创建多少个链接,默认1024
}


http {
    include       mime.types; #引入另外的配置文件,mime.types类型文件
    default_type  application/octet-stream; #默认的类型

    #charset utf-8; #默认编码

    #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  logs/access.log  main;

    sendfile        on;  #数据零拷贝
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65; #保持链接超时时间

    #gzip  on;

  	# 一个server代表一个虚拟主机(vhost)
    server {
        listen       80; #监听端口号,不同的虚拟主机端口号不一样
        server_name  localhost; #当前主机的主机名字,可以配置域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
				# uri,http://www.baidu.com/xxooo/index.html,uri匹配域名之后的资源
        location / {
            root   html; #html相对路径,相对/usr/local/nginx,安装路径
            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; #服务器错误时,转到/50x.html
        location = /50x.html {
            root   html; # 从html目录下找50x.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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

6.虚拟主机与域名配置和站点配置

本地修改hosts文件,添加:
192.168.0.215 www.jeff.com
浏览器测试:输入www.jeff.com,跳转到nginx

创建测试资源

mkdir /data
mkdir /data/image
mkdir /data/movie
vi /data/image/index.html:写入测试信息
vi /data/movie/index.html:写入测试信息

nginx最小配置

server_name可以配置多个域名
注意:从上至下依次匹配server_name,若都没有匹配上则默认第一个server
worker_processes  1; # 启动进程数量,对应内核数量最佳

#事件驱动
events {
    worker_connections  1024; # 每一个进程可以创建多少个链接,默认1024
}

http {
    include       mime.types; #引入另外的配置文件,mime.types类型文件
    default_type  application/octet-stream; #默认的类型
    sendfile        on;  #数据零拷贝
    keepalive_timeout  65; #保持链接超时时间

  	# 虚拟主机1(vhost)
    server {
        listen       80; #监听端口号,不同的虚拟主机端口号不一样
        server_name  www.jeff.com www.jeff2.com; #当前主机的主机名字,可以配置域名

				# http://www.jeff.com/
        location / {
            root   /data/movie; #绝对路径
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html; #服务器错误时,转到/50x.html
        location = /50x.html {
            root   html; # 从html目录下找50x.html
        }
    }

    # 虚拟主机2(vhost)
    server {
        listen       81; #监听端口号,不同的虚拟主机端口号不一样
        server_name  www.jeff.com; #当前主机的主机名字,可以配置域名

				# http://www.jeff.com:81/
        location / {
            root   /data/image; #绝对路径
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html; #服务器错误时,转到/50x.html
        location = /50x.html {
            root   html; # 从html目录下找50x.html
        }
    }
}

浏览器测试

nginx从入门到入坟-LMLPHP

nginx从入门到入坟-LMLPHP

6.1server_name配置规则

//1.完整匹配,域名可以有多个,用空格隔开
server_name  www.jeff.com www.jeff2.com; //server_name可以配置多个域名

//2.通配符匹配
server_name *.jeff.com*

//3.正则匹配
server_name ~^[0-9]+\.jeff\.com$

7.反向代理负载均衡配置

正向代理:代理浏览器,请求的时候用不同的ip请求
反向代理:代理服务器,转发请求到不同的服务器,不同的服务

7.1负载均衡-proxy_pass配置

http://www.jeff.com/代理转发到http://www.baidu.com
注意:proxy_pass写到location里面,下面root等信息就不生效了
302临时重定向
proxy_pass:域名/ip都可以

nginx.conf

worker_processes  1; # 启动进程数量,对应内核数量最佳

#事件驱动
events {
    worker_connections  1024; # 每一个进程可以创建多少个链接,默认1024
}

http {
    include       mime.types; #引入另外的配置文件,mime.types类型文件
    default_type  application/octet-stream; #默认的类型
    sendfile        on;  #数据零拷贝
    keepalive_timeout  65; #保持链接超时时间

  	# 虚拟主机1(vhost)
    server {
        listen       80; #监听端口号,不同的虚拟主机端口号不一样
        server_name  www.jeff.com www.jeff2.com; #当前主机的主机名字,可以配置域名

				# http://www.jeff.com/
        location / {
          proxy_pass http://www.baidu.com;
            #root   /data/movie; #绝对路径
            #index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html; #服务器错误时,转到/50x.html
        location = /50x.html {
            root   html; # 从html目录下找50x.html
        }
    }
}

7.2负载均衡-weight权重配置/down下线配置/backup备用机器配置

# upstream配置与server同级

格式:
proxy_pass http://别名

# 轮训转发,不能放自己负载均衡器的nginx,默认情况下使用轮询方式,逐一转发,这种方式适用于无状态请求。
upstream 别名{
  server 192.168.0.216;
  server 192.168.0.217;
}

# 配置权重,weight权重,比列:3:7
upstream 别名{
  server 192.168.0.216:80 weight=3;
  server 192.168.0.217:80 weight=7;
}

# down不参与负载均衡,(机器挂了),不建议使用down
upstream 别名{
  server 192.168.0.216:80 weight=3;
  server 192.168.0.217:80 weight=7 down;
}

# backup备用机器,当其他机器全部挂了之后就用backup备用机器
upstream 别名{
  server 192.168.0.216:80 weight=3;
  server 192.168.0.217:80 weight=7 backup;
}


down:表示当前的server暂时不参与负载
weight:默认为1.weight越大,负载的权重就越大。
backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。

nginx.conf

worker_processes  1; # 启动进程数量,对应内核数量最佳

#事件驱动
events {
    worker_connections  1024; # 每一个进程可以创建多少个链接,默认1024
}

http {
    include       mime.types; #引入另外的配置文件,mime.types类型文件
    default_type  application/octet-stream; #默认的类型
    sendfile        on;  #数据零拷贝
    keepalive_timeout  65; #保持链接超时时间

    upstream httpds {
    server 192.168.0.216:80 weight=3;
  	server 192.168.0.217:80 weight=7;
  	}

  	# 虚拟主机1(vhost)
    server {
        listen       80; #监听端口号,不同的虚拟主机端口号不一样
        server_name  www.jeff.com; #当前主机的主机名字,可以配置域名

				# http://www.jeff.com/
        location / {
          proxy_pass http://httpds;
            #root   /data/movie; #绝对路径
            #index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html; #服务器错误时,转到/50x.html
        location = /50x.html {
            root   html; # 从html目录下找50x.html
        }
    }
}

7.3ip_hash配置

ip_hash:不常用,根据ip地址转发到同一台服务器,实现保持会话。
比如:登陆nginx转发到1号服务器,此时登陆会话在1号服务器。登陆成功之后nginx就转发到2号服务器。又需要重新登陆。因为nginx是轮训转发。
解决:使用ip_hash,相同的ip转发到同一台服务器。
问题:手机是移动的,ip是可变的,就不能解决此问题。所以不推荐使用ip_hash
最终解决:服务应该是无状态的,登陆token不保存在1号服务器,应该保存到数据库

#每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
upstream bakend {
    ip_hash;
    server 192.168.0.14:88;
    server 192.168.0.15:80;
}

7.4least_conn配置

least_conn:最少连接访问,转发到最少连接到服务器

7.5负载均衡-动静分离

#所有静态文件由nginx直接读取不经过tomcat或resin
总结:静态资源从负载均衡服务器直接获取,动态的资源才从分发服务器获取

7.5.1-动静分离-location普通配置

# /usr/local/nginx/html/images
location /images {
}
# /usr/local/nginx/html/css
location /css {
   expires 15d; #缓存有效期
}
# /usr/local/nginx/html/js
location /js {
}

nginx.conf:

worker_processes  1; # 启动进程数量,对应内核数量最佳

#事件驱动
events {
    worker_connections  1024; # 每一个进程可以创建多少个链接,默认1024
}

http {
    include       mime.types; #引入另外的配置文件,mime.types类型文件
    default_type  application/octet-stream; #默认的类型
    sendfile        on;  #数据零拷贝
    keepalive_timeout  65; #保持链接超时时间

    upstream httpds {
    server 192.168.0.216:80;
  	server 192.168.0.217:80;
  	}

  	# 虚拟主机1(vhost)
    server {
        listen       80; #监听端口号,不同的虚拟主机端口号不一样
        server_name  www.jeff.com; #当前主机的主机名字,可以配置域名

        location / {
          proxy_pass http://httpds;
        }
        # /usr/local/nginx/html/images
    	  location /images {
        }
        # /usr/local/nginx/html/css
        location /css {
        }
        # /usr/local/nginx/html/js
        location /js {
        }

        error_page   500 502 503 504  /50x.html; #服务器错误时,转到/50x.html
        location = /50x.html {
            root   html; # 从html目录下找50x.html
        }
    }
}

7.5.2-动静分离-location正则匹配

#普通location:
location /js {
  root   html; #相对ngin安装路径
  index  index.html index.htm;
}

#正则匹配location:
location ~*/(js|images|css) {
}

#正则匹配location:
location ~ .*.(js|images|css)$ {
  expires 15d; #缓存有效期
}

nginx.conf

worker_processes  1; # 启动进程数量,对应内核数量最佳

#事件驱动
events {
    worker_connections  1024; # 每一个进程可以创建多少个链接,默认1024
}

http {
    include       mime.types; #引入另外的配置文件,mime.types类型文件
    default_type  application/octet-stream; #默认的类型
    sendfile        on;  #数据零拷贝
    keepalive_timeout  65; #保持链接超时时间

    upstream httpds {
    server 192.168.0.216:80;
  	server 192.168.0.217:80;
  	}

  	# 虚拟主机1(vhost)
    server {
        listen       80; #监听端口号,不同的虚拟主机端口号不一样
        server_name  www.jeff.com; #当前主机的主机名字,可以配置域名

        location / {
          proxy_pass http://httpds;
        }
        #正则匹配
        location ~*/(js|images|css) {
        }
        error_page   500 502 503 504  /50x.html; #服务器错误时,转到/50x.html
        location = /50x.html {
            root   html; # 从html目录下找50x.html
        }
    }
}

8.防盗链配置

valid_referers server_names:不带参数配置
valid_referers none server_names:检测Referer头域不存在的情况也可以访问
valid_referers blocked server_names:Referer不以“http://” 或 “https://” 开头也可以访问。

//参数说明:
none:检测 Referer 头域不存在的情况。
blocked:检测 Referer头域的值被防火墙或者代理服务器删除或伪装的情况。这种情况该头域的值不以“http://” 或 “https://” 开头。
server_names :设置一个或多个域名 ,检测Referer头域的值是否是这些URL中的某一个。
#浏览器header中referer:来源网址,设置防盗链
#检测浏览器referer是否为www.jeff.com
valid_referers  www.jeff.com; if
($invalid_referer)  {
return 403;
}

#正则匹配location:
location ~*/(js|images|css) {
  valid_referers  www.jeff.com; if
  ($invalid_referer)  {
   return 403;
   }
  root   html; #相对ngin安装路径
  index  index.html index.htm;
}

# 403错误配置方式一,加上403
error_page   403 500 502 503 504  /50x.html;
# 403错误配置方式二
#正则匹配location:rewrite返回错误图片
location ~*/(js|images|css) {
  valid_referers  192.168.0.215; if
  ($invalid_referer)  {
   rewrite ^/ /images/err.png break;
   }
}

nginx.conf

worker_processes  1; # 启动进程数量,对应内核数量最佳

#事件驱动
events {
    worker_connections  1024; # 每一个进程可以创建多少个链接,默认1024
}

http {
    include       mime.types; #引入另外的配置文件,mime.types类型文件
    default_type  application/octet-stream; #默认的类型
    sendfile        on;  #数据零拷贝
    keepalive_timeout  65; #保持链接超时时间

    upstream httpds {
    server 192.168.0.216:80;
  	server 192.168.0.217:80;
  	}

  	# 虚拟主机1(vhost)
    server {
        listen       80; #监听端口号,不同的虚拟主机端口号不一样
        server_name  www.jeff.com; #当前主机的主机名字,可以配置域名

        location / {
          proxy_pass http://httpds;
        }
        #正则匹配
        location ~*/(js|images|css) {
           valid_referers  192.168.0.215; if
          ($invalid_referer)  {
           return 403;
           }
        }
        error_page   403 500 502 503 504  /50x.html; #服务器错误时,转到/50x.html
        location = /50x.html {
            root   html; # 从html目录下找50x.html
        }
    }
}

9.nginx高可用keepalived配置

keepalived原理说明:一个虚拟ip绑定在nginx上,如果主nginx挂了,则ip换绑在其他nginx

# 安装keepalived
yum install keepalived
#启动
systemctl start keepalived
systemctl status keepalived
systemctl stop keepalived

#keepalived选举方式
priority优先级选举

主nginx

# 配置文件
vi /etc/keepalived/keepalived.conf

router_id nginx1 # router_id不能一样
vrrp_instance VI_1 {
    state MASTER
    interface eth0 # ip addr查看网卡名称
    virtual_router_id 51
    priority 100 #优先级,主备竞选时,数值大的是主
    advert_int 1 #间隔检测时间
    authentication { #认证配置
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.0.100 # 虚拟ip,以后外网访问的ip
    }
}

备用nginx

# 配置文件
vi /etc/keepalived/keepalived.conf

router_id nginx2 # router_id不能一样
vrrp_instance VI_1 {
    state BACKUP
    interface eth0 # ip addr查看网卡名称
    virtual_router_id 51
    priority 50 #优先级,主备竞选时,数值大的是主
    advert_int 1 #间隔检测时间
    authentication { #认证配置
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.0.100
    }
}

10.nginx域名证书配置https

第一步:阿里云申请域名,并绑定服务器
第二步:阿里云申请CA证书,并下载nginx的CA证书
第三步:下载的证书上传到nginx/conf目录下
第四步:nginx.conf添加一个server

nginx.conf

# 虚拟主机1(vhost)
server {
  listen       443 ssl; #监听端口号,不同的虚拟主机端口号不一样
  server_name  loaclhost; #当前主机的主机名字,可以配置域名

  ssl_certificate 123456_www.jeff.com.pem;  #相对路径从conf目录中找
  ssl_certificate_key 123456_www.jeff.com.key;
}

systemctl reload nginx //重新加载配置文件

再次访问时:http提示不安全,https安全
选择了IT,必定终身学习
04-25 18:47