nginx常用命令

  1. # ./sbin/nginx -h
  2. nginx version: nginx/1.2.4
  3. Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]
  4. Options:
  5. -?,-h : this help 显示该帮助信息
  6. -v : show version and exit 打印版本号并退出
  7. -V : show version and configure options then exit打印版本号和配置并退出
  8. -t : test configuration and exit 测试配置正确性并退出
  9. -q : suppress non-error messages during configuration testing 测试
  10. 配置时只显示错误
  11. -s signal : send signal to a master process: stop, quit, reopen, reload向主
  12. 进程发送信号
  13. -p prefix : set prefix path (default: /Nginx/bin/) 指定Nginx服务器路径前缀
  14. -c filename : set configuration file (default: conf/nginx.conf) 指定Nginx
  15. 置文件路径
  16. -g directives : set global directives out of configuration file 指定Nginx
  17. 加配置文件路径

nginx搭建生产可用nacos

  1. nacos有内嵌数据库,nacos支持mysql,这里我们将存储改为mysql。修改nacos的/conf/application.properties ```properties spring.datasource.platform=mysql

db.num=1

Connect URL of DB:

db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC db.user.0=root db.password.0=U9gGfFgGJGyXYi9c

  1. 2. 配置集群修改nacos的/conf/cluster.conf,填写内网不同服务器的ip。说一个用外网搭建的情况,cluster.conf会自动补上该服务器的内网ip,如果是3台外网的,那么就有6ip(因为会自动写入内网ip)。
  2. ```properties
  3. 192.168.0.247:8401
  4. 192.168.0.241:8402
  5. 192.168.0.244:8403
  1. nacos占用内存过大时可以改nacos目录下的/bin/startup.sh

    1. JAVA_OPT="${JAVA_OPT} -server -Xms512m -Xmx512m -Xmn256m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
  2. nginx负载均衡到每一台nacos,修改/etc/nginx/nginx.conf

    1. stream {
    2. upstream nacos {
    3. server 192.168.0.247:8401;
    4. server 192.168.0.241:8402;
    5. server 192.168.0.244:8403;
    6. }
    7. server {
    8. listen 18848; #需要监听的端口
    9. proxy_timeout 20s;
    10. proxy_pass nacos;
    11. }
    12. }

    保存后重新加载一下nginx配置

    1. sudo systemctl reload nginx

    这是我搭建出来的效果图
    image.png

Keepalived Nginx高可用

对照这个文档配置https://support.huaweicloud.com/bestpractice-vpc/bestpractice_0010.html
监控脚本中如果直接复制进去执行是会失败的,会提示这个异常yntax error: end of file unexpected (expecting “then”) 出现问题的原因是.sh文件是dos格式文件,但是linux的shell需要unix格式的文件,因此需要进行转换,解决方法如下:

  1. sudo apt-get install dos2unix
  2. dos2unix <filename>

还有一个注意的地方,master上面执行ip addr show应该是能看到虚拟的ip,现在把master执行这条systemctl stop keepalived.service 停止掉这台的,现在到backup的机器上面看,执行ip addr show 就能看到虚拟ip,keepalived帮我们完成了自动切换。同理master执行systemctl start keepalived.service,backup执行systemctl stop keepalived.service,就会自动切换到master为当前的主服务,记得将虚拟ip绑定公网,华为一个坑就是不绑定公网就无法访问外网。

配置stream时异常

启动nginx时报错 nginx: [emerg] unknown directive “stream” in /etc/nginx/nginx.conf:13

  1. yum install nginx-mod-stream