Nginx CookBook 手册 高性能HTTP和反向代理服务器

在这个开发即运维的时代,Nginx 属于必会技能。

Nginx特点

(1)反向代理
(2)负载均衡
(3)动静分离

安装Nginx

安装Nginx依赖包:

yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

Nginx下载地址:https://nginx.org/

下载,解压,配置检查

wget https://nginx.org/download/nginx-1.20.1.tar.gz
tar -zxvf nginx-1.20.1.tar.gz
cd nginx-1.20.1
./configure

可以看到一些有用的信息:

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

安装Nginx :

make && make install 

启动Nginx

cd /usr/local/nginx/sbin
./nginx

看看有没有启动成功:
可以看看进程:

ps -aux|grep nginx

在这里插入图片描述

或者可以直接访问80端口:
因为cat /usr/local/nginx/conf/nginx.conf 里面写了:
在这里插入图片描述
浏览器输入 IP:80
在这里插入图片描述
搞定CentOS防火墙:
查看防火墙状态

firewall-cmd --state

停止firewall

systemctl stop firewalld.service

禁止firewall开机启动

systemctl disable firewalld.service 

查看防火墙开放的端口号:

firewall-cmd --list-all

设置开放防火墙端口号:

firewall-cmd --add-service=http --permanent
firewall-cmd --add-port=80/tcp --permanent

重启防火墙:

firewall-cmd --reload

常用命令

需要进入到sbin目录中去执行这些命令,其实就是sbin里面有nginx入口。

cd /usr/local/nginx/sbin

基本参数:

(base) [root@localhost sbin]# ./nginx -h
nginx version: nginx/1.20.1
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
             [-e filename] [-c filename] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -e filename   : set error log file (default: logs/error.log)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

基本命令:

./nginx -v # 查看版本号
./nginx -s stop # 停止nginx
./nginx -s reload # 重加载nginx,配置文件会重新加载生效
./nginx  # 直接启动nginx

Nginx 配置文件内容

vim /usr/local/nginx/conf/nginx.conf

全局块:
在这里插入图片描述
events块:
在这里插入图片描述
http块:
其中又含有http全局块和http的server块。
在这里插入图片描述

Nginx 配置反向代理-实例

docker run -d -p 8080:8080 tomcat  # 在Linux中启动一个tomcat服务

vim /usr/local/nginx/conf/nginx.conf # 编辑配置文件

在这里插入图片描述

./nginx -s reload

这个时候再访问这台服务器的80端口的服务,会看到是tomcat的服务页面。

Nginx 配置

参考:https://www.runoob.com/w3cnote/nginx-setup-intro.html
参考:https://www.nginx.cn/doc/

########### 每个指令必须有分号结束。#################
#user administrator administrators;  #配置用户或者组,默认为nobody nobody。
#worker_processes 2;  #允许生成的进程数,默认为1
#pid /nginx/pid/nginx.pid;   #指定nginx进程运行文件存放地址
error_log log/error.log debug;  #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
events {
    
    
    accept_mutex on;   #设置网路连接序列化,防止惊群现象发生,默认为on
    multi_accept on;  #设置一个进程是否同时接受多个网络连接,默认为off
    #use epoll;      #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
    worker_connections  1024;    #最大连接数,默认为512
}
http {
    
    
    include       mime.types;   #文件扩展名与文件类型映射表
    default_type  application/octet-stream; #默认文件类型,默认为text/plain
    #access_log off; #取消服务日志    
    log_format myFormat '$remote_addr$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
    access_log log/access.log myFormat;  #combined为日志格式的默认值
    sendfile on;   #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
    sendfile_max_chunk 100k;  #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
    keepalive_timeout 65;  #连接超时时间,默认为75s,可以在http,server,location块。

    upstream mysvr {
    
       
      server 127.0.0.1:7878;
      server 192.168.10.121:3333 backup;  #热备
    }
    error_page 404 https://www.baidu.com; #错误页
    server {
    
    
        keepalive_requests 120; #单连接请求上限次数。
        listen       4545;   #监听端口
        server_name  127.0.0.1;   #监听地址       
        location  ~*^.+$ {
    
           #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
           #root path;  #根目录
           #index vv.txt;  #设置默认页
           proxy_pass  http://mysvr;  #请求转向mysvr 定义的服务器列表
           deny 127.0.0.1;  #拒绝的ip
           allow 172.18.5.54; #允许的ip           
        } 
    }
}

Nginx配置高可用集群

Nginx原理

Guess you like

Origin blog.csdn.net/x1131230123/article/details/121184110