1,nginx安装(目录为/usr/src/)

1,CentOS上传下载文件命令:

  1. yum -y install lrzsz

2,安装依赖:pcre
1)首先去官网下载压缩包文件
其他的source网站需要墙,点击https://www.pcre.org/,我使用的ftp下载的
https://ftp.pcre.org/pub/pcre/pcre-8.37.tar.gz
https://ftp.pcre.org/pub/pcre/
yum安装:yum install -y pcre pcre-devel

  1. # 检查各依赖是否安装
  2. gcc -v
  3. rpm -qa pcre
  4. yum list installed | grep zlib*
  5. rpm -qa openssl
  6. # 安装依赖
  7. yum install gcc-c++
  8. yum install -y pcre pcre-devel
  9. yum install -y zlib zlib-deve
  10. yum install -y openssl openssl-devel
  1. # 编译安装
  2. # 获取安装包
  3. wget https://ftp.pcre.org/pub/pcre/pcre-8.37.tar.gz
  4. # 解压缩
  5. tar -xzvf pcre-8.37.tar.gz
  6. # 进入该目录
  7. cd pcre-8.37
  8. # 运行configure,后面的两个参数之间没有空格
  9. ./configure --enable-utf8
  10. # 执行configure命令后如果缺少编译依赖,可根据提示安装
  11. # 例如:configure: error: You need a C++ compiler for C++ support.
  12. # yum install -y gcc gcc-c++
  13. # 执行make命令
  14. make && make install
  15. # 查看是否安装成功
  16. pcre-config --version

3,安装依赖:openssl,

  1. yum install -y openssl openssl-devel

4,安装依赖:zlib

  1. yum install -y zlib zlib-devel

5,最后安装:nginx

  1. # 上传安装包到指定目录,解压
  2. tar -xzvf nginx-1.20.1.tar.gz
  3. # 切换到解压目录,执行
  4. cd nginx-1.20.1
  5. ./configure
  6. # 执行make命令
  7. make && make install

image.png
安装后会打印出 nginx的安装目录 /usr/local/nginx 以及配置文件目录/usr/local/nginx/conf/nginx.conf

2,操作nginx

1,初次启动
cd /usr/local/nginx/sbin
执行指令: ./nginx

2,停止nginx
cd /usr/local/nginx/sbin
执行指令:./nginx -s stop

3,重启nginx,重新加载配置文件
cd /usr/local/nginx/sbin
执行指令:./nginx -s reload

4,查看nginx是够启动
ps -ef|grep nginx

5,查看版本
cd /usr/local/nginx/sbin
执行指令: ./nginx -v
执行指令: ./nginx -version

3,配置反向代理,配置文件目录/usr/local/nginx/conf/nginx.conf

  1. location / {
  2. root html;
  3. # 反向代理配置proxy_pass
  4. proxy_pass http://127.0.0.1:8080;
  5. index index.html index.htm;
  6. }
  1. server {
  2. listen 8000;
  3. server_name 192.168.0.121;
  4. location ~ /edu/ {
  5. proxy_pass http://127.0.0.1:8001;
  6. }
  7. location ~ /vod/ {
  8. proxy_pass http://127.0.0.1:8002;
  9. }
  10. }
  11. # ~ 表示uri包含正则表达式,并且区分大小写
  12. # ~* 表示uri包含正则表达式,并且不区分大小写
  13. # = 表示uri不包含正则表达式,请求字符串必须严格匹配
  14. # ^~ 表示uri不包含正则表达式,请求字符串前面必须匹配

4,配置负载均衡

image.png

  1. http {
  2. include mime.types;
  3. default_type application/octet-stream;
  4. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  5. # '$status $body_bytes_sent "$http_referer" '
  6. # '"$http_user_agent" "$http_x_forwarded_for"';
  7. #access_log logs/access.log main;
  8. sendfile on;
  9. #tcp_nopush on;
  10. #keepalive_timeout 0;
  11. keepalive_timeout 65;
  12. upstream mybackend {
  13. server 192.168.0.121:8001;
  14. server 192.168.0.121:8002;
  15. }
  16. #gzip on;
  17. server {
  18. listen 8000;
  19. server_name 192.168.0.121;
  20. location / {
  21. proxy_pass http://mybackend;
  22. root html;
  23. index index.html index.htm;
  24. }