跳至主要內容

nginx

常用工具中间件安装nginx约 579 字大约 2 分钟

1. 下载

进入官网open in new window下载对应版本,本文以nginx-1.18.0.tar.gz为例。

下载
下载

2. 安装

  1. 安装前置依赖
yum -y install gcc
yum install -y pcre pcre-develyum install -y pcre pcre-devel
yum install -y zlib zlib-devel
  1. 将压缩包进行解压
tar -xf nginx-1.18.0.tar.gz
解压
解压
  1. 编译安装Nginx
./configure
make && make install
  1. 配置环境变量
export NGINX_HOME="安装路径" 
PATH=$PATH:$NGINX_HOME/sbin
export PATH
配置环境变量
配置环境变量
  1. 验证安装是否成功
nginx -v
验证
验证
  1. 修改相关配置
cat /usr/local/nginx/conf/nginx.conf
location /jerry-ui {
	alias  /data/jerry-ui;
}

location /nacos {
  proxy_redirect off;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_pass http://127.0.0.1:8848/nacos;
}

location /logikm {
  rewrite ^/logikm/(.*)$ /$1 break;
  proxy_pass          http://127.0.0.1:30001;
  proxy_http_version  1.1;
  proxy_set_header    Upgrade $http_upgrade;
  proxy_set_header    Connection "upgrade";
  proxy_set_header    Host $host;
  proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header    X-Real-IP  $remote_addr;
  proxy_set_header    X-Forwarded-Proto  $scheme;
}

location ^~ /sentinel/ {
  proxy_next_upstream error timeout http_503 http_504 http_502;
  proxy_connect_timeout 500s;
  proxy_read_timeout 500s;
  proxy_send_timeout 500s;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_pass http://127.0.0.1:18080/;
}
配置文件
配置文件

3. 其他

常用命令

#重启Nginx
nginx -s reopen 

#重新加载Nginx配置文件,然后以优雅的方式重启Nginx
nginx -s reload 

#强制停止Nginx服务
nginx -s stop 

#优雅地停止Nginx服务(即处理完所有请求后再停止服务)
nginx -s quit 

#检测配置文件是否有语法错误,然后退出
nginx -t 

#打开帮助信息
nginx -?,-h 

#显示版本信息并退出
nginx -v 

#显示版本和配置选项信息,然后退出
nginx -V 

#检测配置文件是否有语法错误,然后退出
nginx -t 

#检测配置文件是否有语法错误,转储并退出
nginx -T 

#在检测配置文件期间屏蔽非错误信息
nginx -q 

#设置前缀路径(默认是:/usr/share/nginx/)
nginx -p prefix 

#设置配置文件(默认是:/etc/nginx/nginx.conf)
nginx -c filename 

#设置配置文件外的全局指令
nginx -g directives 

#杀死所有nginx进程
killall nginx 

生成自签证书

openssl genrsa -des3 -out cert.key 2048
openssl req -new -key cert.key -out cert.csr
openssl rsa -in cert.key -out cert.key
openssl x509 -req -days 365 -in cert.csr -signkey cert.key -out cert.crt
openssl x509 -in cert.crt -out cert.pem -outform PEM

配置密码访问

htpasswd -c /etc/nginx/auth/admin admin 

#添加以下两行,第一行为提示内容,第二行为密码文件路径
auth_basic "请输入用户名和密码"; 
auth_basic_user_file /etc/nginx/auth/admin;