010_Nginx入门


使用场景

Tomcat 默认配置的最大请求数是 150,并发量小,用户使用的少,在低并发的状况下,一个jar包启动应用就够了,而后内部tomcat返回内容给用户。
image.png
用户愈来愈多了,并发量慢慢增大了,这时候一台服务器知足不了咱们的需求了。
image.png
因而咱们横向扩展,又增长了服务器。这个时候几个项目启动在不一样的服务器上,用户要访问,就须要增长一个代理服务器了,经过代理服务器来帮咱们转发和处理请求。
image.png
咱们但愿这个代理服务器能够帮助咱们接收用户的请求,而后将用户的请求按照规则帮咱们转发到不一样的服务器节点之上。这个过程用户是无感知的,用户并不知道是哪一个服务器返回的结果,咱们还但愿他能够按照服务器的性能提供不一样的权重选择。保证最佳体验!因此咱们使用了Nginx。
php

什么是Nginx

Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。2011年6月1日,nginx 1.0.4发布。
其特色是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。在全球活跃的网站中有12.18%的使用比率,大约为2220万个网站。
Nginx 是一个安装很是的简单、配置文件很是简洁(还可以支持perl语法)、Bug很是少的服务。Nginx 启动特别容易,而且几乎能够作到7*24不间断运行,即便运行数个月也不须要从新启动。你还可以不间断服务的状况下进行软件版本的升级。
Nginx代码彻底用C语言从头写成。官方数据测试代表可以支持高达 50,000 个并发链接数的响应。
css

正向代理与反向代理

正向代理:代理客户端

image.png

反向代理:代理服务端

image.png

Nginx的做用

经过使用Nginx,大大提升网站的响应速度,优化用户体验,让网站的健壮性更上一层楼!
html

反向代理

Http代理,反向代理:做为web服务器最经常使用的功能之一,尤为是反向代理。
linux

负载均衡

Nginx提供的负载均衡策略有2种:内置策略和扩展策略。内置策略为轮询,加权轮询,Ip hash。扩展策略,就天马行空,只有你想不到的没有他作不到的。
nginx

轮询

image.png

加权轮询

image.png

IP hash

iphash对客户端请求的ip进行hash操做,而后根据hash结果将同一个客户端ip的请求分发给同一台服务器进行处理,能够解决session不共享的问题。
image.png
c++

动静分离

动静分离,在咱们的软件开发中,有些请求是须要后台处理的,有些请求是不须要通过后台处理的(如:css、html、jpg、js等等文件),这些不须要通过后台处理的文件称为静态文件。让动态网站里的动态网页根据必定规则把不变的资源和常常变的资源区分开来,动静资源作好了拆分之后,咱们就能够根据静态资源的特色将其作缓存操做。提升资源响应的速度。
image.png
web

Windows下安装

下载Nginx

http://nginx.org/en/download.html 下载稳定版本。
image.png
以nginx/Windows-1.18.0为例,直接下载 nginx-1.18.0.zip。
下载后解压,解压后以下:
image.png
正则表达式

启动Nginx

有不少种方法启动nginx算法

  1. 直接双击nginx.exe,双击后一个黑色的弹窗一闪而过
  2. 打开cmd命令窗口,切换到nginx解压目录下,输入命令 nginx.exe ,回车即

image.png

检查Nginx是否启动成功

直接在浏览器地址栏输入网址 http://localhost:80 回车,出现如下页面说明启动成功!
image.png
shell

配置监听

nginx的配置文件是conf目录下的nginx.conf,默认配置的nginx监听的端口为80,若是80端口被占用能够修改成未被占用的端口便可。
当咱们修改了nginx的配置文件nginx.conf 时,不须要关闭nginx后从新启动nginx,只须要执行命令 nginx -s reload 便可让改动生效

server {
        listen       80;
        server_name  localhost;
    }

关闭Nginx

若是使用cmd命令窗口启动nginx, 关闭cmd窗口是不能结束nginx进程的,可以使用两种方法关闭nginx

  1. 输入nginx命令 nginx -s stop(快速中止nginx) 或 nginx -s quit(完整有序的中止nginx)
  2. 使用taskkill taskkill /f /t /im nginx.exe
taskkill是用来终止进程的
/f是强制终止
/t终止指定的进程和任何由此启动的子进程。
/im示指定的进程名称

Linux下安装

安装gcc

安装 nginx 须要先将官网下载的源码进行编译,编译依赖 gcc 环境,若是没有 gcc 环境,则须要安装:

yum install gcc-c++

PCRE pcre-devel 安装

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,因此须要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也须要此库。命令:

yum install -y pcre pcre-devel

zlib 安装

zlib 库提供了不少种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,因此须要在 Centos 上安装 zlib 库。

yum install -y zlib zlib-devel

OpenSSL 安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、经常使用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不只支持 http 协议,还支持 https(即在ssl协议上传输http),因此须要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel

下载Nginx

手动下载.tar.gz安装包,地址:https://nginx.org/en/download.html
下载完毕上传到服务器上
image.png
image.png

解压

tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0


配置/编译/安装

./configure
make
make install

查找安装路径: whereis nginx

启动Nginx

cd /usr/local/nginx/sbin/
./nginx  启动

检查Nginx是否启动成功

启动成功,访问 服务器ip:80,直接在浏览器地址栏输入网址 http://192.168.10.216:80 回车,出现如下页面说明启动成功!
image.png

注意:如何链接不上,检查服务器是否开放端口,或者服务器防火墙是否开放端口!

防火墙相关命令

# 开启防火墙
service firewalld start
# 重启
service firewalld restart
# 关闭
service firewalld stop
# 查看防火墙规则
firewall-cmd --list-all
# 查询端口是否开放
firewall-cmd --query-port=8080/tcp
# 开放80端口
firewall-cmd --permanent --add-port=80/tcp
# 移除端口
firewall-cmd --permanent --remove-port=8080/tcp
#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload
# 参数解释
一、firwall-cmd:是Linux提供的操做firewall的一个工具;
二、--permanent:表示设置为持久;
三、--add-port:标识添加的端口;

Nginx经常使用命令

cd /usr/local/nginx/sbin/
./nginx  启动
./nginx -s stop  中止
./nginx -s quit  安全退出
./nginx -s reload  从新加载配置文件
ps aux|grep nginx  查看nginx进程

Nginx配置反向代理

location / {
            root   html;
            index  index.html index.htm;
						# 反向代理配置
						proxy_pass http://qing;
        }

Nginx配置负载均衡

# 负载均衡配置,名字qing可任意定义,在server->location->proxy_pass反向代理配置中使用
	# weight是权重配置
	upstream qing {
		server 192.168.10.216:8080 weight=1;
		server 192.168.10.217:8080 weight=2;
	}

Nginx配置文件nginx.conf

  1. 全局配置
  2. http配置
  3. http服务配置
  4. https服务配置
  5. 负载均衡配置
  6. 反向代理配置
# 全局配置
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

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

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
	
	# 负载均衡配置,名字qing可任意定义,在server->location->proxy_pass反向代理配置中使用
	# weight是权重配置
	upstream qing {
		server 192.168.10.216:8080 weight=1;
		server 192.168.10.217:8080 weight=2;
	}

	# http服务配置
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
			# 反向代理配置
			proxy_pass http://qing;
        }

        #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   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
    # https服务配置
    #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配置详情

相关文章
相关标签/搜索