nginx部署

nginx部署

1、下载与解压nginx

sudo wget http://nginx.org/download/nginx-1.18.0.tar.gz

sudo tar -zxvf nginx-1.18.0.tar.gz 

2、nginx依赖安装

sudo yum install -y pcre-devel
sudo yum install -y openssl openssl-devel 

3、编译与安装nginx

进入解压后目录
cd  nginx-1.18.0/
configure
sudo ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

–prefix 指定安装路径

–with-http_stub_status_module 允许查看nginx状态的模块

–with-http_ssl_module 支持https的模块

编译并安装
sudo make 
sudo make install
sudo chmod 755 -R /usr/local/nginx/

3、测试编译安装的效果

查看nginx的版本
/usr/local/nginx/sbin/nginx -v

结果

nginx version: nginx/1.18.0

4、查看nginx的配置编译参数

/usr/local/nginx/sbin/nginx -V

结果

nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

注意区分和上一条查看版本命令的区别: -v参数分别是小写和大写

4、配置nginx服务

运行的准备工作:配置日志目录
sudo mkdir /data/nginx
sudo mkdir /data/nginx/logs
sudo chmod 755 -R /data/nginx/
运行的准备工作:创建nginx用户
sudo groupadd nginx

sudo useradd -g nginx -s /sbin/nologin -M nginx 

#-g:指定所属的group

#-s:指定shell,因为它不需要登录,所以用/sbin/nologin

#-M:不创建home目录,因为它不需要登录

简单配置nginx
sudo vi nginx.conf

指定运行nginx的用户和组是:nginx

user nginx nginx;

发生错误时要写入到错误日志(目录用上面创建好的)

error_log /data/nginx/logs/error.log;

指定pid的路径

pid logs/nginx.pid;

日志格式(取消注释即可)

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  /data/nginx/logs/access.log  main;
生成service文件
sudo vi /usr/lib/systemd/system/nginx.service

内容

[Unit]
Description=nginx-The High-performance HTTP Server
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
启动服务

重新加载服务文件

sudo systemctl daemon-reload 

启动:

sudo systemctl start nginx
查看效果:

从浏览器访问安装机器的ip的80端口即可:

查看日志目录
ll /data/nginx/logs/

结果

total 8
-rw-r----- 1 root root 920 Apr 28 17:36 access.log
-rw-r----- 1 root root 274 Apr 28 17:36 error.log

日志已成功写入

5、修改Nignx缺省banner

修改nginx源代码
sudo vi /home/lin/nginx-1.18.0/src/http/ngx_http_header_filter_module.c

修改前代码

static u_char ngx_http_server_string[] = "Server: nginx" CRLF;
static u_char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
static u_char ngx_http_server_build_string[] = "Server: " NGINX_VER_BUILD CRLF;

修改后代码

static u_char ngx_http_server_string[] = "Server: unkowna" CRLF;
static u_char ngx_http_server_full_string[] = "Server: unkowna " CRLF;
static u_char ngx_http_server_build_string[] = "Server: unkowna" CRLF;
configure(参照安装时的参数)
sudo ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
编译
sudo make 
首次安装执行安装
sudo make install
已安装的替换nginx

备份

sudo cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

停止服务

sudo systemctl stop nginx.service

拷贝新的编译的nginx

sudo cp /home/lin/nginx-1.18.0/objs/nginx /usr/local/nginx/sbin/nginx
修改nginx配置

在nginx.conf的http标签中添加server_tokens off;

sudo vi /usr/local/nginx/conf/nginx.conf

修改前

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  /data/nginx/logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

修改后

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  /data/nginx/logs/access.log  main;

server_tokens off;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

启动nginx

sudo systemctl start nginx.service

通过curl查看结果

curl -I http://127.0.0.1:9191


部署前

curl -I http://127.0.0.1:9191
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Thu, 29 Apr 2021 00:54:39 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 28 Apr 2021 09:19:30 GMT
Connection: keep-alive
ETag: "608928a2-264"
Accept-Ranges: bytes


部署后效果

HTTP/1.1 200 OK
Server: unkowna
Date: Thu, 29 Apr 2021 01:22:58 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 28 Apr 2021 09:19:30 GMT
Connection: keep-alive
ETag: “608928a2-264”
Accept-Ranges: bytes

7、配置反向代理

Nginx 代理服务的配置说明
  • 设置 404 页面导向地址

    error_page 404 /404.html; #错误页
    proxy_intercept_errors on; #如果被代理服务器返回的状态码为400或者大于400,设置的error_page配置起作用。默认为off。

  • 代理只允许接受get

    proxy_method get;#支持客户端的请求方法

  • 设置支持的http协议版本

    proxy_http_version 1.0 ; #Nginx服务器提供代理服务的http协议版本1.0,1.1,默认设置为1.0版本

  • 关于代理配置的配置文件部分,仅供参考

    include mime.types; #文件扩展名与文件类型映射表
    default_type application/octet-stream; #默认文件类型,默认为text/plain
    #access_log off; #取消服务日志
    log_format myFormat ’ r e m o t e a d d r – remote_addr– remoteaddrremote_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块。
    proxy_connect_timeout 1; #nginx服务器与被代理的服务器建立连接的超时时间,默认60秒
    proxy_read_timeout 1; #nginx服务器想被代理服务器组发出read请求后,等待响应的超时间,默认为60秒。
    proxy_send_timeout 1; #nginx服务器想被代理服务器组发出write请求后,等待响应的超时间,默认为60秒。
    proxy_http_version 1.0 ; #Nginx服务器提供代理服务的http协议版本1.0,1.1,默认设置为1.0版本。
    #proxy_method get; #支持客户端的请求方法。post/get;
    proxy_ignore_client_abort on; #客户端断网时,nginx服务器是否终端对被代理服务器的请求。默认为off。
    proxy_ignore_headers “Expires” “Set-Cookie”; #Nginx服务器不处理设置的http相应投中的头域,这里空格隔开可以设置多个。
    proxy_intercept_errors on; #如果被代理服务器返回的状态码为400或者大于400,设置的error_page配置起作用。默认为off。
    proxy_headers_hash_max_size 1024; #存放http报文头的哈希表容量上限,默认为512个字符。
    proxy_headers_hash_bucket_size 128; #nginx服务器申请存放http报文头的哈希表容量大小。默认为64个字符。
    proxy_next_upstream timeout; #反向代理upstream中设置的服务器组,出现故障时,被代理服务器返回的状态值。error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off
    #proxy_ssl_session_reuse on; 默认为on,如果我们在错误日志中发现“SSL3_GET_FINSHED:digest check failed”的情况时,可以将该指令设置为off。

Nginx代理服务配置参考(验证通过)
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  /data/nginx/logs/access.log  main;

server_tokens off;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

gzip  on;
client_max_body_size 50m;  #缓冲区代理缓冲用户端请求的最大字节数,可以理解为保存到本地再传给用户
client_body_buffer_size 256k;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
proxy_connect_timeout 300s; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_read_timeout 300s; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_send_timeout 300s;
proxy_buffer_size 64k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k;  #设定缓存文件夹大小,大于这个值,将从upstream服务器传递请求,而不缓冲到磁盘
proxy_ignore_client_abort on; #不允许代理端主动关闭连接

server {
    listen       80;
    server_name  base.sdchedu.cn;

   # 同步请求
   location /sync {
        proxy_pass       http://10.30.211.105:8080; #服务空中课堂访问地址
        proxy_redirect   off;
        proxy_set_header Host                        $host;                      # header添加请求host信息
        proxy_set_header X-Real-IP                   $remote_addr;               # header增加请求来源IP信息
        proxy_set_header X-Forwarded-For             $proxy_add_x_forwarded_for; # 增加代理记录
        add_header       Access-Control-Allow-Origin *;
    }

   #拦截所有请求
   location /upload {
        proxy_pass http://10.30.211.113:8080; #服务空中课堂访问地址
        proxy_redirect off;
        proxy_set_header Host $host; #header添加请求host信息
        proxy_set_header X-Real-IP $remote_addr; # header增加请求来源IP信息
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 增加代理记录
        add_header Access-Control-Allow-Origin *;
    }
}

server {
    listen       80;
    server_name  dm.chsedu.cn;
   #拦截所有请求
   location / {
        proxy_pass http://IP:8080; #代码服务
        allow 223.78.120.140;#ip白名单
        allow 223.78.120.141;#ip白名单
        deny all;#白名单意外IP禁止访问
        proxy_redirect off;
        proxy_set_header Host $host; #header添加请求host信息
        proxy_set_header X-Real-IP $remote_addr; # header增加请求来源IP信息
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 增加代理记录
        add_header Access-Control-Allow-Origin *;
    }

}
}

8、配置负载均衡

负载均衡原理
  • 原理说明

如果你的nginx服务器给2台web服务器做代理,负载均衡算法采用轮询,那么当你的一台机器web程序iis关闭,也就是说web不能访问,那么nginx服务器分发请求还是会给这台不能访问的web服务器,如果这里的响应连接时间过长,就会导致客户端的页面一直在等待响应,对用户来说体验就打打折扣,这里我们怎么避免这样的情况发生呢。这里我配张图来说明下问题。

image

如果负载均衡中其中web2发生这样的情况,nginx首先会去web1请求,但是nginx在配置不当的情况下会继续分发请求道web2,然后等待web2响应,直到我们的响应时间超时,才会把请求重新分发给web1,这里的响应时间如果过长,用户等待的时间就会越长。

下面的配置是解决方案之一。

proxy_connect_timeout 1;   #nginx服务器与被代理的服务器建立连接的超时时间,默认60秒
proxy_read_timeout 1; #nginx服务器想被代理服务器组发出read请求后,等待响应的超时间,默认为60秒。
proxy_send_timeout 1; #nginx服务器想被代理服务器组发出write请求后,等待响应的超时间,默认为60秒。
proxy_ignore_client_abort on;  #客户端断网时,nginx服务器是否终端对被代理服务器的请求。默认为off。
  • 如果使用upstream指令配置啦一组服务器作为被代理服务器,服务器中的访问算法遵循配置的负载均衡规则,同时可以使用该指令配置在发生哪些异常情况时,将请求顺次交由下一组服务器处理。

    proxy_next_upstream timeout; #反向代理upstream中设置的服务器组,出现故障时,被代理服务器返回的状态值。

状态值可以是:

error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off

error:建立连接或向被代理的服务器发送请求或读取响应信息时服务器发生错误。
timeout:建立连接,想被代理服务器发送请求或读取响应信息时服务器发生超时。
invalid_header:被代理服务器返回的响应头异常。
off:无法将请求分发给被代理的服务器。
http_400,....:被代理服务器返回的状态码为400,500,502,等。
  • 如果你想通过http获取客户的真是ip而不是获取代理服务器的ip地址,那么要做如下的设置。

    proxy_set_header Host KaTeX parse error: Expected 'EOF', got '#' at position 7: host; #̲只要用户在浏览器中访问的域名绑…host ;host是访问URL中的域名和端口 www.taobao.com:80
    proxy_set_header X-Real-IP KaTeX parse error: Expected 'EOF', got '#' at position 15: remote_addr; #̲把源IP 【remote_addr,建立HTTP连接header里面的信息】赋值给X-Real-IP;这样在代码中 $X-Real-IP来获取 源IP
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#在nginx 作为代理服务器时,设置的IP列表,会把经过的机器ip,代理机器ip都记录下来,用 【,】隔开;代码中用 echo $x-forwarded-for |awk -F, ‘{print $1}’ 来作为源IP
    关于X-Forwarded-For与X-Real-IP的一些相关文章可以查看:HTTP 请求头中的 X-Forwarded-For

Nginx负载均衡方式介绍
  • 轮询

轮询方式是Nginx负载默认的方式,顾名思义,所有请求都按照时间顺序分配到不同的服务上,如果服务Down掉,可以自动剔除,如下配置后轮训10001服务和10002服务。

upstream  backend-server {
       server    localhost:10001;
       server    localhost:10002;
}
  • 权重

指定每个服务的权重比例,weight和访问比率成正比,通常用于后端服务机器性能不统一,将性能好的分配权重高来发挥服务器最大性能,如下配置后10002服务的访问比率会是10001服务的二倍。

参考1

upstream  backend-server {
       server    localhost:10001 weight=1 ;
       server    localhost:10002 weight=2;
}

参考2

upstream  backend-server {
        #server 192.168.10.11:8080 weight=2 max_fails=2 fail_timeout=2;
        #server 192.168.10.12:8080 weight=1 max_fails=2 fail_timeout=1;    
}
  • iphash

每个请求都根据访问ip的hash结果分配,经过这样的处理,每个访客固定访问一个后端服务,如下配置(ip_hash可以和weight配合使用)

upstream  backend-server {
       ip_hash; 
       server    localhost:10001 weight=1;
       server    localhost:10002 weight=2;
}
  • url_hash

按访问url的hash结果来分配请求,使每个url定向到同一个(对应的)后端服务器,后端服务器为缓存时比较有效

upstream backend-server {
    server localhost:10001;
    server localhost:10002;
    hash $request_uri;
    hash_method crc32;
}
  • fair

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

upstream  backend-server {
       server    localhost:10001 weight=1;
       server    localhost:10002 weight=2;
       fair;  
}
  • 最少连接

将请求分配到连接数最少的服务上

upstream  backend-server {
       least_conn;
       server    localhost:10001 weight=1;
       server    localhost:10002 weight=2;
}
Nginx配置参考

以轮训为例

http {
upstream  backend-server {
   server    localhost:10001;
   server    localhost:10002;
}
server {
   listen       10000;
   server_name  localhost;

   location / {
    proxy_pass http://backend-server1;
    proxy_redirect default;
  }
}
server {
   listen       10002;
   server_name  localhost;

   location / {
    proxy_pass http://backend-server2;
    proxy_redirect default;
  }
}
}

9、更新了ssh和ssl后安装nginx

安装nginx——ssl插件

修改配置文件
vi /root/nginx-1.18.0/auto/lib/openssl/conf

说明 :

修改1:

CORE_INCS="$CORE_INCS $OPENSSL/openssl/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/openssl/include/openssl/ssl.h"

if [ -f $OPENSSL/ms/do_ms.bat ]; then
    # before OpenSSL 1.1.0
    CORE_LIBS="$CORE_LIBS $OPENSSL/openssl/lib/ssleay32.lib" //修改lib为lib64
    CORE_LIBS="$CORE_LIBS $OPENSSL/openssl/lib/libeay32.lib"
else
    # OpenSSL 1.1.0+
    CORE_LIBS="$CORE_LIBS $OPENSSL/openssl/lib/libssl.lib"
    CORE_LIBS="$CORE_LIBS $OPENSSL/openssl/lib/libcrypto.lib"
fi

修改为

CORE_INCS="$CORE_INCS $OPENSSL/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/include/openssl/ssl.h"

if [ -f $OPENSSL/ms/do_ms.bat ]; then
    # before OpenSSL 1.1.0
    CORE_LIBS="$CORE_LIBS $OPENSSL/lib64/ssleay32.lib"
    CORE_LIBS="$CORE_LIBS $OPENSSL/lib64/libeay32.lib"
else
    # OpenSSL 1.1.0+
    CORE_LIBS="$CORE_LIBS $OPENSSL/lib64/libssl.lib"
    CORE_LIBS="$CORE_LIBS $OPENSSL/lib64/libcrypto.lib"
fi

修改2

CORE_INCS="$CORE_INCS $OPENSSL/.openssl/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/.openssl/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libcrypto.a"

修改为:

CORE_INCS="$CORE_INCS $OPENSSL/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/lib64/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/lib64/libcrypto.a"
配置编译安装
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-openssl=/usr/local

make
make install

参考

centos8平台编译安装nginx1.18.0

https://www.cnblogs.com/architectforest/p/12755195.html

详细记录一次Tomcat服务器和Nginx服务器的缺省banner的修改全过程

https://blog.csdn.net/honyer455/article/details/86491269

Nginx实现负载均衡

https://www.jianshu.com/p/4c250c1cd6cd

Nginx 反向代理与负载均衡详解

https://www.runoob.com/w3cnote/nginx-proxy-balancing.html

Nginx 配置详解

https://www.runoob.com/w3cnote/nginx-setup-intro.html

Nginx如何封禁IP和IP段的实现

https://www.jb51.net/article/190752.htm

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值