下载地址:http://nginx.org/en/download.html解压:tar -xf nginx-1.21.6.tar.gz安装依赖:yum install -y gccyum install -y pcre pcre-develyum install -y zlib zlib-devel安装:./configure --prefix=/usr/local/nginxmakemake install到此安装完成注意:要关闭防火墙systemctl stop firewalld.servicesystemctl disable --now firewalld
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 重新加载配置,修改配置文件之后使用
vi /usr/lib/systemd/system/nginx.service[Unit]Description=nginx - web serverAfter=network.target remote-fs.target nss-lookup.target[Service]Type=forkingPIDFile=/usr/local/nginx/logs/nginx.pidExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.confExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s stopExecQuit=/usr/local/nginx/sbin/nginx -s quitPrivateTmp=true[Install]WantedBy=multi-user.target//重新加载系统服务systemctl daemon-reload//开机启动nginxsystemctl enable nginx.service//脚本启动systemctl start nginx //启动systemctl stop nginx //关闭systemctl status nginx //状态systemctl restart nginx //重启systemctl reload nginx //重新加载配置文件
drwx------. 2 nobody root 6 4月 19 09:22 client_body_tempdrwxr-xr-x. 2 root root 4096 4月 19 09:20 confdrwx------. 2 nobody root 6 4月 19 09:22 fastcgi_tempdrwxr-xr-x. 2 root root 40 4月 19 09:20 htmldrwxr-xr-x. 2 root root 58 4月 19 10:16 logsdrwx------. 2 nobody root 6 4月 19 09:22 proxy_tempdrwxr-xr-x. 2 root root 19 4月 19 09:20 sbindrwx------. 2 nobody root 6 4月 19 09:22 scgi_tempdrwx------. 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启动的进程号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; # } #}}本地修改hosts文件,添加:192.168.0.215 www.jeff.com浏览器测试:输入www.jeff.com,跳转到nginx创建测试资源
mkdir /datamkdir /data/imagemkdir /data/movievi /data/image/index.html:写入测试信息vi /data/movie/index.html:写入测试信息nginx最小配置
server_name可以配置多个域名注意:从上至下依次匹配server_name,若都没有匹配上则默认第一个serverworker_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 } }}浏览器测试


//1.完整匹配,域名可以有多个,用空格隔开server_name www.jeff.com www.jeff2.com; //server_name可以配置多个域名//2.通配符匹配server_name *.jeff.com*//3.正则匹配server_name ~^[0-9]+\.jeff\.com$正向代理:代理浏览器,请求的时候用不同的ip请求反向代理:代理服务器,转发请求到不同的服务器,不同的服务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 } }}# upstream配置与server同级格式:proxy_pass http://别名# 轮训转发,不能放自己负载均衡器的nginx,默认情况下使用轮询方式,逐一转发,这种方式适用于无状态请求。upstream 别名{ server 192.168.0.216; server 192.168.0.217;}# 配置权重,weight权重,比列:3:7upstream 别名{ server 192.168.0.216:80 weight=3; server 192.168.0.217:80 weight=7;}# down不参与负载均衡,(机器挂了),不建议使用downupstream 别名{ 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 } }}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;}least_conn:最少连接访问,转发到最少连接到服务器#所有静态文件由nginx直接读取不经过tomcat或resin总结:静态资源从负载均衡服务器直接获取,动态的资源才从分发服务器获取# /usr/local/nginx/html/imageslocation /images {}# /usr/local/nginx/html/csslocation /css { expires 15d; #缓存有效期}# /usr/local/nginx/html/jslocation /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 } }}#普通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 } }}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.comvalid_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错误配置方式一,加上403error_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 } }}keepalived原理说明:一个虚拟ip绑定在nginx上,如果主nginx挂了,则ip换绑在其他nginx# 安装keepalivedyum install keepalived#启动systemctl start keepalivedsystemctl status keepalivedsystemctl 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 }}第一步:阿里云申请域名,并绑定服务器第二步:阿里云申请CA证书,并下载nginx的CA证书第三步:下载的证书上传到nginx/conf目录下第四步:nginx.conf添加一个servernginx.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安全