Nginx使用说明

📌Nginx (engine x) 是一个轻量级高性能的HTTP和反向代理服务器,同时也是一个通用 代理服务器(TCP/UDP/IMAP/POP3/SMTP),最初由俄罗斯人Igor Sysoev编写。

常用命令

# 使用systemctl启动、停止、重新加载
systemctl start nginx	# 启动nginx
systemctl status nginx	# 查看nginx状态
systemctl stop nginx	# 停止nginx
systemctl reload ningx	# 重新加载配置文件
systemctl enable nginx	# 配置开机启动

# 进入到nginx/sbin/目录下
nginx -V			# 查看版本
ningx -t			# 检查配置文件是否有语法错误
nginx				# 启动
nginx -s reload		# 热加载,重新加载配置文件
nginx -s stop		# 快速关闭
nginx -s quit		# 等待工作进程处理完毕后关闭

配置文件

配置文件位置:/etc/nginx/nginx.conf
为了保持主配置文件的简洁,同时配置多个.conf文件方便区分,增加可读性,可以使用系列命令引用/etc/nginx/conf.d目录下的所有.conf文件

include /etc/nginx/conf.d/*.conf;

动静分离、ssl证书配置

server {
    listen 80;
    #请填写绑定证书的域名
    server_name xxx.com;
    #把http的域名请求转成https
    return 301 https://$host$request_uri;
}

server {
    charset utf-8;
    client_max_body_size 128M;

    # 由于版本问题,配置文件可能存在不同的写法。例如:Nginx 版本为 nginx/1.15.0 以上请使用 listen 443 ssl 代替 listen 443 和 ssl on
    listen 443 ssl;
    server_name xxx.com;
    # 将name.pem替换成您证书的文件名称。
    ssl_certificate /data/nginx/conf.d/cert/name.pem;
    # 将name.key替换成您证书的密钥文件名称。
    ssl_certificate_key /data/nginx/conf.d/cert/name.key;
    ssl_session_timeout 5m;
    # 使用此加密套件。
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    # 使用该协议进行配置。
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    # 配置站点根目录
    root    /data/html/dist;
    index   index.html;

    access_log  /var/log/nginx_access.log;
    error_log   /var/log/nginx_error.log;

    # 前端文件
    location / {
        # Redirect everything that isn't a real file to index.html
        try_files $uri $uri/ /index.html$is_args$args;
        index  index.html index.htm;
    }

    location ~* /\. {
        deny all;
    }

    # 代理
    location /prod-api/ {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://ip:port/;
    }

    # 后端上传的文件
    location /img/ {
        proxy_pass http://ip:port/;
    }

    # websocket配置,需要在nginx.conf 中的 http 配置如下
    #     map $http_upgrade $connection_upgrade {
    #     default upgrade;
    #     '' close;
    # }
    location /websocket {
        proxy_pass http://ip:port/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }

    location /xxxx.txt/ {
        proxy_pass http://http://ip:port/statics/web-view/xxx.txt;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root html;
    }
}

安装

官方地址:http://nginx.org

# 1、安装依赖包
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

# 2、下载安装包
wget http://nginx.org/download/nginx-1.18.0.tar.gz

# 3、解压到自定义位置
tar -zxvf nginx-1.18.0.tar.gz  -C  /opt

# 4、进入解压目录
cd /opt/nginx-1.18.0

# 5、创建安装目录
mkdir -p /usr/local/nginx

# 6、指定安装目录
./configure --prefix=/usr/local/nginx

# 7、编译安装
make && make install

# 8、配置路径
vim /etc/profile
  # 在原有的PATH之上加上/usr/local/nginx/sbin:
  # 例如:
  # export PATH=/usr/local/nginx/sbin:$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH

# 9、安装完毕

注:正式环境安装还需要其它一下设置,如设置用户组等

posted @ 2023-05-19 00:02  The Answer  阅读(20)  评论(0编辑  收藏  举报