nginx从安装到使用(这一篇就够了)

本篇文章适合正在学习Nginx的小伙伴儿,以及正在找工作的大佬面试使用。如果文章有写的不准确或需要改进的地方,还请各位大佬不吝赐教💞💞💞。朱七在此先感谢大家了。😘😘😘
🏠个人主页:语雀个人知识库
🧑个人简介:大家好,我是嚼绿箭的朱七,一个想要与大家共同进步的程序猿😉😉
💕欢迎大家:这里是CSDN,我总结知识的地方,欢迎来到我的博客,我亲爱的大佬😘
————————————————

第一章 安装
安装nginx
yum update	
yum install gcc openssl openssl-devel pcre pcre-devel zlib zlib-devel -y

cd /root/opt/mysoftwares
tar -zxvf nginx.tar

cd nginx

./configure --prefix=/usr/local/nginx

make & make install

cd /usr/local/nginx

cd sbin
ls

# nginx

#运行
./nginx

#检测
ps -ef|grep nginx

# master worker 进程代表成功
启动Nginx

进入到sbin目录下

./nginx

通过配置文件启动

./nginx -c /usr/local/nginx/conf/nginx.conf
关闭nginx
第一种 优雅的关闭
ps -ef|grep nginx
kill -quit pid
第二种 快速关闭
ps -ef|grep nginx
kill -term pid
重启nginx
./nginx -s reload
配置检查
./nginx -c /usr/local/nginx/conf/nginx.conf -t

-t: 进行配置检查

查看版本
#查看版本号
./nginx -v
#查看版本号,编译版本,配置信息
./nginx -V
第二章 配置文件
#配置worker进程运行用户,nobody也是一个linux用户,一般用于启动程序,没有密码
#user  nobody;

#配置工作进程数目,根据硬件调整,通常等于cpu数量或者2倍
worker_processes  1;

#配置全局错误日志及类型[debug | info | notice | warn | error | crit],默认是error
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#保存pid的文件
#pid        logs/nginx.pid;


#配置工作模式和连接数
events {
    worker_connections  1024;#配置每个worker进程的连接数上限,nginx支持的总连接数就等于改数乘工作进程数目,最大为65535
}

#配置http服务器,利用它的反向代理功能提供负载均衡支持
http {
	#配置nginx支持哪些多媒体类型,可以在conf/mime.types查看支持哪些多媒体类型
    include       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日志及存放路径,并使用上面的main日志格式,访问日志
    #access_log  logs/access.log  main;

    sendfile        on;#开启高效文件传输模式
    #tcp_nopush     on;#防止网络阻塞,上线一般开启

    #keepalive_timeout  0;
    keepalive_timeout  65;#长连接超时时间,单位是秒

    #gzip  on;#开启gzip压缩输出,上线一般开启

	#配置虚拟主机
    server {
        listen       80;#配置监听端口
        server_name  localhost;#配置服务名

        #charset koi8-r; #配置字符集,默认是utf-8

        #access_log  logs/host.access.log  main; #访问日志,虚拟主机的

		#默认的匹配斜杠/的请求,当访问路径中有斜杠/,会被该location匹配到并进行处理
        location / {
            root   html;#root是配置服务器的默认网站跟目录位置,默认为nginx安装主目录下的html目录
            index  index.html index.htm;#默认的首页文件的名称
        }

        #error_page  404              /404.html; #配置404页面

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   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;
    #    }
    #}

}
第三章 静态网页部署

直接改nginx.conf中的location,把静态资源上传上去就ok了

第四章 负载均衡
硬件负载

f5 Arrary等

性能好等优点

费用贵

软件负载

Nginx等

免费开源

启动tomcat
./apache-tomcat-9.0.60-01/bin/startup.sh | tail -f ./apache-tomcat-9.0.60-01/logs/catalina.out &
第五章 nginx常用负载均衡策略
轮询(默认)
权重
upstream www.myweb.com{
     server ***.***.**.***:8081 weight 3;
	server ***.***.**.***:8082 weight 4;
          }

    server {
        listen       80;
        server_name  localhost; 
        #server_name本地域名
  
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

	location /myweb {
	    proxy_pass http://www.myweb.com;
	}
ip_hash

通过ip的hash%tomcat数 相同 ,访问tomcat

一次连接后,永远不变

upstream www.myweb.com{
	ip_hash;
     server ***.***.**.***:8081;
	server ***.***.**.***:8082;
}
fair

按后端服务器的响应时间来分配请求,响应时间短的优先分配

upstream server pool{
server 192.168.5.21:80
server 192.168.5.22:80
fair
}
最少连接
upstream www.myweb.com{
     least conn;
     server ***.***.**.***:8081;
	server ***.***.**.***:8082;
}
备份
upstream www.myweb.com{
     least conn;
     server ***.***.**.***:8081;
	server ***.***.**.***:8082 backup;
}
第六章 静态代理

用nginx 静态代理,效率高

第一种 后缀名
location ~ .*\.(css|htm|html|js|jpg|png|map3)${
		root /opt/static;
}
第二种 目录
location ~ .*/(css|js|img|images|image){
		root /opt/static;
}
第七章 动静分离

image.png
从实现的角度上大致上分为两种:
一种是纯粹把静态文件独立成单独的域名,放在独立的服务器上。
另一种就是动态跟静态文件混合在一起发布,通过nginx来分开
通过location指定不同的后缀名实现不同的请求转发。通过expire参数设置,可以使浏览器缓冲过期时间,减少与服务器之间的请求和流量。我们这里设置3d,表示这3天之内访问这个URI,发送一个请求,比对服务器该文件最后的更新时间没有变化
nginx只用与负载均衡

然后其他nginx负载静态资源

tomcat负载动态资源

   #tomcat
   upstream www.p2p.com{
             ip_hash;
             server xxx.xxx.xxx.xxx:8081;
            server xxx.xxx.xxx.xxx:8082;
    }	

	#处理静态资源的nginx
    upstream static.p2p.com{
	server xxx.xxx.xxx.xxx:81;
	server xxx.xxx.xxx.xxx:82;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

		#动态资源转发给tomcat
        location /p2p {
            proxy_pass http://www.p2p.com;
        }

		#静态资源转发给nginx
        location  ~ .*/(css|js|img|images){
                 proxy_pass http://static.p2p.com;
        }
第八章 虚拟主机
upstream beijing.myweb.com {
		server 192.168.1.101:8081;
	}

	upstream nanjing.myweb.com {
		server 192.168.1.101:8082;
	}

	upstream tianjing.myweb.com {
		server 192.168.1.101:8083;
	}


	server{
		listen	80;
		server_name	beijing.myweb.com;

		location / {
			proxy_pass http://beijing.myweb.com;
		}
	}

	server{
		listen	80;
		server_name	tianjing.myweb.com;

		location / {
			proxy_pass http://tianjing.myweb.com;
		}
	}

	server{
		listen	80;
		server_name	nanjing.myweb.com;

		location / {
			proxy_pass http://nanjing.myweb.com;
		}
	}

配置本地dns映射

在c盘的windows文件夹中的system32中的drivers中的etc中的hosts文件

C:\Windows\System32\drivers\etc

添加本地ip DNS 映射

第八章 高可用

image.png
高可用
image.png

安装keeyalived

1.使用yum命令进行安装
yum install keepalived -y
2.安装之后,在etc里面生成目录keepalived,有文件keepalived.conf

完成高可用配置(主从配置)
静态资源最大并发数是:worker_connectionworker_process/2,
HTTP为反向代理来说,最大并发数量应该是worker_connections
worker_process/4

  • 19
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嚼绿箭的朱七

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值