Nginx安装、使用及配置详解

本问包含Nginx的安装、配置、功能、原理及使用方式

Nginx安装、使用及配置详解

  • 使用brew下载安装,没有brew请先安装神器brew
brew search nginx
brew install nginx
  • 启动
sudo nginx -s start
或者
nginx
  • 结束nginx
sudo nginx -s stop
或者
ps -ef | grep nginx   这里针对不同系统查看进程的指令不同
找到nginx:master的pid
Kill pid

重启

sudo nginx -s reload
或者
cd /usr/local/Cellar/nginx/1.15.12/bin
./nginx -s reload  

怎样配置Nginx

  • 首先找到Nginx的配置文件nginx/nginx.conf,mac系统nginx路径为/usr/local/etc/nginx/nginx.conf
  • 按照下面配置文件介绍的方式修改本地的配置文件,确保文件保存成功
  • 重启Nginx

配置文件详解,可以对比本地的nginx配置文件,

#定义Nginx运行的用户和用户组,来指定Nginx Worker进程运行用户以及用户组,默认由nobody账号运行
user ***;

#nginx进程数,建议设置为等于CPU总核心数。
worker_processes 4;

#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ],其中debug输出日志最为最详细,而crit输出日志最少
error_log logs/error.log info;

#进程文件,用来指定进程id的存储文件位置
pid logs/nginx.pid;

#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致,可以使用命令“ulimit -n 65535”来设置其他值。
worker_rlimit_nofile 4864;

#工作模式与连接数上限
events {
    #参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
    use epoll; #mac平台用kqueue,对于Linux系统,epoll工作模式是首选
    #worker_connections用于定义Nginx每个进程的最大连接数,即接收前端的最大请求数,默认是1024。最大客户端连接数由worker_processes和worker_connections决定,即Max_clients=worker_processes*worker_connections,在作为反向代理时,Max_clients变为:Max_clients = worker_processes * worker_connections/4。 进程的最大连接数受Linux系统进程的最大打开文件数限制,在执行操作系统命令“ulimit -n 65536”后worker_connections的设置才能生效
    worker_connections 1024;
}

#设定http服务器
http {
    #来用设定文件的mime类型,类型在配置文件目录下的mime.type文件定义,来告诉nginx来识别文件类型。
    include mime.types; 
    default_type application/octet-stream; #默认文件类型
    #charset utf-8; #默认编码
    #用于设置日志的格式,和记录哪些参数,这里设置为main,刚好用于access_log来纪录这种类型
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #'$status $body_bytes_sent "$http_referer" '
    #'"$http_user_agent" "$http_x_forwarded_for"';
    sendfile on; #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
    autoindex on; #开启目录列表访问,合适下载服务器,默认关闭。
    tcp_nopush on; #防止网络阻塞
    tcp_nodelay on; #防止网络阻塞
    keepalive_timeout 120; #长连接超时时间,单位是秒

#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;

#gzip模块设置
    gzip on; #开启gzip压缩输出
    gzip_min_length 1k; #最小压缩文件大小
    gzip_buffers 4 16k; #压缩缓冲区
    gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
    gzip_comp_level 2; #压缩等级
    gzip_types text/plain application/x-javascript text/css application/xml;
    #压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
    gzip_vary on;
    #limit_zone crawler $binary_remote_addr 10m; #开启限制IP连接数的时候需要使用

    upstream blog.ha97.com {
    #upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
        server 192.168.80.121:80 weight=3;
        server 192.168.80.122:80 weight=2;
        server 192.168.80.123:80 weight=3;
    }
    #虚拟主机的配置
    server {
        #监听端口
        listen 80;
        #域名可以有多个,用空格隔开
        server_name www.***.com ***.com;
        index index.html index.htm index.php;
        #表示在这整个server虚拟主机内,全部的root web根目录。注意要和locate {}下面定义的区分开来
        root /data/www/***;
        location /mp/ {
             proxy_pass http://127.0.0.1:8080;
             proxy_set_header     Host $host;
        }
        location / {
             proxy_pass http://192.168.200.248:80;
             proxy_set_header     Host $host;
        }
        add_header Access-Control-Allow-Origin "*";
    }
}
  • 代理及负载均衡配置详解
upstream tomcatserver1 {  
    server 192.168.72.49:9090 down;   
    server 192.168.72.49:8080 weight=2;   
    server 192.168.72.49:6060;   
    server 192.168.72.49:7070 backup; 
    }   
  
 server {  
        listen       80;  
        server_name  8080.max.com;  
        #charset koi8-r;  
        #access_log  logs/host.access.log  main;  
        location / {  
            proxy_pass   http://tomcatserver1;  
            index  index.html index.htm;  
        }  
     }

1)down
    表示单前的server暂时不参与负
2)Weight
    默认为1.weight越大,负载的权重就越大。
3)max_fails
    允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
4)fail_timeout
    max_fails 次失败后,暂停的时间。
5)Backup
    其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

Nginx两大核心功能—>反向代理和负载均衡

反向代理的概念:

一、反向代理(Reverse Proxy)方式最简单的理解方式:
	1)、用户访问我们在Nginx中配置的server和port的地址,我们Nginx把请求转发给upstream中配置或proxy_pass配置的IP+PORT
	2)、后台的Tomcat(或者其他服务器)返回的数据返回的对象是Nginx(Nginx提供缓存的功能),Nginx再将数据给客户端
	
二、为了对比引入另一个概念 -> 代理(正向代理)

	1)、正向代理(forward proxy) ,一个位于客户端和原始服务器之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并制定目标(原始服务器),然后代理向原始服务器转发请求并将获得的内容返回给客户端,客户端才能使用正向代理。我们平时说的代理就是指正向代理。 
    2)、简单一点:A向C借钱,由于一些情况不能直接向C借钱,于是A想了一个办法,他让B去向C借钱,这样B就代替A向C借钱,A就得到了C的钱,C并不知道A的存在,B就充当了A的代理人的角色。
一、负载均衡的概念
 1、转发功能
	按照一定的算法【权重、轮询】,将客户端请求转发到不同应用服务器上,减轻单个服务器压力,提高系统并发量。
 2、故障移除
	通过心跳检测的方式,判断应用服务器当前是否可以正常工作,如果服务器期宕掉,自动将请求发送到其他应用服务器。
 3、恢复添加
	如检测到发生故障的应用服务器恢复工作,自动将其添加到处理用户请求队伍中。

二、Nginx实现负载均衡
假设使用两个tomcat模拟两台应用服务器,端口号分别为8080 和8081
	 upstream tomcatserver1 {  
   	 		server 192.168.72.49:8080 weight=3;  
    		server 192.168.72.49:8081;  
    	} 
   那么通过轮询知道目前转发的服务器有两台,根据weight实现8080的机器处理大部分的请求,weight根据机器的性能进行配置
 1、Nginx的负载分发策略--->upstream
  Nginx 的 upstream目前支持的分配算法: 
	1)、轮询 ——1:1 轮流处理请求(默认)
      每个请求按时间顺序逐一分配到不同的应用服务器,如果应用服务器down掉,自动剔除,剩下的继续轮	询。 
	2)、权重 ——you can you up
      通过配置权重,指定轮询几率,权重和访问比率成正比,用于应用服务器性能不均的情况。 
	3)、ip_哈希算法
      每个请求按访问ip的hash结果分配,这样每个访客固定访问一个应用服务器,可以解决session共享的问题。 

 2、配置Nginx的负载均衡与分发策略
      通过在upstream参数中添加的应用服务器IP后添加指定参数即可实现,如:

项目启动命令行的Log

  • 我们在安装Nginx时,我们可以仔细阅读,这里可以得到很多的信息,包括版本、安装路径等信息,私下可以认真读一下
==> Installing dependencies for nginx: openssl and pcre
==> Installing nginx dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2r.high_sierra.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/2b/2b68bd92c0c2faea5a1e70cc57a2403482ab2d83d0201bb42016c57c754427a5?__gda__=exp=1557390493~hmac=d81c3db66021f7642507b8368f4bca21d08a5b661cde47cba53c
######################################################################## 100.0%
==> Pouring openssl-1.0.2r.high_sierra.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

openssl is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have openssl first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile

For compilers to find openssl you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl/include"

==> Summary
?  /usr/local/Cellar/openssl/1.0.2r: 1,795 files, 12.1MB
==> Installing nginx dependency: pcre
==> Downloading https://homebrew.bintray.com/bottles/pcre-8.43.high_sierra.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/03/0389911a93a88efd4a69b52dea8ecb872fdb55bcfff45d2f7313be5f79730861?__gda__=exp=1557390513~hmac=72b4367c2b6f03b61a956745380934d8e1aabd0617bea4482085
######################################################################## 100.0%
==> Pouring pcre-8.43.high_sierra.bottle.tar.gz
?  /usr/local/Cellar/pcre/8.43: 204 files, 5.5MB
==> Installing nginx
==> Downloading https://homebrew.bintray.com/bottles/nginx-1.15.12.high_sierra.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/29/29dcd1272f76f9f4a6e0af7260e58631a34dfe1690e358c3af0a5a926a7dced8?__gda__=exp=1557390520~hmac=94c40cc45d12cea90a5e11d3e8261778927d0a4f1170fcd0e1db
######################################################################## 100.0%
==> Pouring nginx-1.15.12.high_sierra.bottle.tar.gz
==> Caveats
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

To have launchd start nginx now and restart at login:
  brew services start nginx
Or, if you don't want/need a background service you can just run:
  nginx
==> Summary
?  /usr/local/Cellar/nginx/1.15.12: 25 files, 2MB
==> `brew cleanup` has not been run in 30 days, running now...
Removing: /Users/wangjunjie/Library/Caches/Homebrew/nginx--1.15.5.high_sierra.bottle.tar.gz... (581.4KB)
Removing: /Users/wangjunjie/Library/Caches/Homebrew/openssl--1.0.2p.high_sierra.bottle.tar.gz... (3.7MB)
Removing: /Users/wangjunjie/Library/Caches/Homebrew/pcre--8.42.high_sierra.bottle.tar.gz... (1.8MB)
Removing: /Users/wangjunjie/Library/Caches/Homebrew/mysql--8.0.12.high_sierra.bottle.tar.gz... (59.4MB)
Removing: /Users/wangjunjie/Library/Logs/Homebrew/nginx... (64B)
Removing: /Users/wangjunjie/Library/Logs/Homebrew/mysql... (1022B)
Removing: /Users/wangjunjie/Library/Logs/Homebrew/pcre... (64B)
Removing: /Users/wangjunjie/Library/Logs/Homebrew/openssl... (64B)
Pruned 0 symbolic links and 3 directories from /usr/local
==> Caveats
==> openssl
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

openssl is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have openssl first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile

For compilers to find openssl you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl/include"

==> nginx
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

To have launchd start nginx now and restart at login:
  brew services start nginx
Or, if you don't want/need a background service you can just run:
  nginx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值