侧边栏壁纸
博主头像
小武哥博主等级

专注做好每一件小事。

  • 累计撰写 45 篇文章
  • 累计创建 58 个标签
  • 累计收到 3 条评论

Nginx一键安装脚本

小武哥
2021-10-31 / 0 评论 / 2 点赞 / 701 阅读 / 2,676 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-02-14,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

把一键执行脚本与nginx-1.18.0.tar.gz安装脚本上传到服务器,执行脚本即可。Nginx服务会以Systemd形式后台运行。

#!/bin/bash
#@author 编程之间
tar -zxvf nginx-1.18.0.tar.gz -C /usr/local/
cd /usr/local/nginx-1.18.0
groupadd www
useradd -g www www
./configure --user=www --group=www --prefix=/usr/local/nginx --with-stream --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-stream_realip_module --with-threads
make
make install
/usr/local/nginx/sbin/nginx -V
cd /lib/systemd/system
echo "
[Unit]
Description=nginx service
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/lolca/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target" >>nginx.service
systemctl enable nginx.service
systemctl start nginx.service

Nginx配置ssl

首先获取ssl证书,这里配置的是从腾讯云获取的免费证书。配置如下

    server {
        listen       443 ssl;
        server_name  pan.codehome.vip;

        ssl_certificate      /usr/local/nginx/ssl/pan.crt;
        ssl_certificate_key  /usr/local/nginx/ssl/pan.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   /var/www/html;
            index  index.html index.htm;
        }
    }

Nginx配置Tcp代理

这里演示是Nginx代理Emqx

stream {
    upstream mqtt1883 {
        server tcpip:tcp端口 weight=1;
        server 172.19.1.4:1883 weight=1;
        server 172.19.1.5:1883 weight=1;
    }
    server {
        listen 1883;
        proxy_pass mqtt1883;
        proxy_buffer_size 3M;
        tcp_nodelay on;
    }
}

Nginx代理websocket

    server {
        listen 8083;
        server_name localhost;
        client_max_body_size 100M;
        location / {
            proxy_redirect off;
            proxy_pass http://127.0.0.1:8083; #这里为http和上面的https有所区别
            proxy_set_header Host $host;
            # 反向代理保留客户端地址
            proxy_set_header X-Real_IP $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr:$remote_port;
            # WebSocket 额外请求头
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }

Nginx代理后台接口

http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 65;
    client_max_body_size 20m;
    #开启zip压缩
    gzip on;
    #大于1k开始压缩
    gzip_min_length 1k;
    #压缩缓冲区大小4个32k的内存作为压缩结果流缓存
    gzip_buffers 4 32k;
    #压缩比例越大越压缩多,但是消耗cpu资源多
    gzip_comp_level 6;
    gzip_types text/css text/xml application/javascript application/json;
    upstream api {
        server 192.30.1.1:9000 max_fails=3 fail_timeout=60s;
        server 192.30.1.2:9000 max_fails=3 fail_timeout=60s;
 
    }


    server {
        listen 80;
        server_name localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        location / {
            root /var/www/html;
            index index.html index.htm;
        }

        location /nginx_status {
                stub_status on;
                access_log off;
        }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }

        location ^~ /api/ {
            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://api/;

        }

    }

}

2

评论区