nginx

系统:CentOS Linux release 7.9.2009 (Core)
一、安装nginx
1、yum安装:
添加yum源

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

安装nginx:
yum install nginx -y
2、源码编译安装
下载nginx稳定版本
http://nginx.org/download/nginx-1.22.0.tar.gz
先安装编译器和依赖包
yum -y install gcc make zlib-devel pcre pcre-devel openssl-devel
解压nginx
unzip nginx-1.22.0.tar.gz

编译nginx
./configure --prefix=/usr/local/nginx --sbin-path=/usr/bin/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream make && make install
--prefix=/usr/local/nginx 包括client_body_temp conf fastcgi_temp html logs proxy_temp scgi_temp uwsgi_temp
--sbin-path=/usr/bin/nginx nginx执行文件的位置

验证nginx

[root@server html]#nginx -V
nginx version: nginx/1.22.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 --sbin-path=/usr/bin/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream

保存以下内容到/lib/systemd/system/nginx.service文件,可使用systemd管理nginx,需要注意nginx文件和pid文件的位置

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/bin/nginx -t
ExecStart=/usr/bin/nginx
ExecReload=/usr/bin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

二、使用nginx
1、web服务器
将静态页面放在html文件夹内即可通过ip地址+文件名访问
2、反向代理
在location区域添加proxy_pass https://www.baidu.com;这一行,浏览器里打开服务器的ip地址就打开百度了

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass https://www.baidu.com;
        }

3、https证书
有多种工具可以生成本地证书,达到https的目的
如果你有域名,可以通过各种云申请一年有效期的免费的https证书,免费证书只验证域名,在域名解析后台添加一个cname记录验证通过就可以马上得到证书文件,下载适合nginx的证书解压出来,将域名bundle.crt域名.key文件上传到服务器中,然后这两个文件的路径配置到nginx.conf文件中,要删除https部分的#注释
使用Https://域名 时,如果有公网ip需要添加a记录,如果没有备案,很快就打不开了,只是测试的话,修改客户端电脑的hosts文件,将域名和服务器ip地址添加进去就可以打开https链接了

    # HTTPS server
    #
    server {
        listen       443 ssl;
        server_name  localhost;

        ssl_certificate      域名bundle.crt;
        ssl_certificate_key  域名.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;
        }
    }

}

4.使用keepalived实现nginx的高可用
两台CentOS 7 分别安装keepalived和nginx
用yum安装简单,安装后keepalived的配置文件在/etc/keepalived/keepalived.conf
yum安装的nginx web目录在/usr/share/nginx/html/

! Configuration File for keepalived

global_defs {

   router_id lb29 //两台主机的router_id不要一样

}

vrrp_instance VI_1 {
    state BACKUP   //MASTER,BACKUP表示主从,正常情况下请求都到master上,如果master挂了,backup自动接管请求
    interface ens32 //ens32是网卡名,根据ip a查看网卡名
    virtual_router_id 51
    priority 50   //数字表示优先级,master大于backup
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.123 //虚拟ip,通过这个ip来发起对nginx的请求,优先将请求定位到MASTER上,如果MASTER挂了,则BACKUP进行请求响应
    }
}

参看链接:http://nginx.org/en/linux_packages.html#RHEL-CentOS

posted @ 2022-06-11 19:13  何方妖孽  阅读(39)  评论(0编辑  收藏  举报