NGINX 使用及部署文档

1. 安装 NGINX

在 Ubuntu 上安装 NGINX
bash 复制代码
sudo apt update
sudo apt install nginx
在 CentOS 上安装 NGINX
bash 复制代码
sudo yum install epel-release
sudo yum install nginx

2. 启动 NGINX

bash 复制代码
sudo systemctl start nginx

3. 基本配置

配置文件位置

NGINX的主要配置文件:/etc/nginx/nginx.conf

默认站点配置

NGINX默认站点配置文件:/etc/nginx/sites-available/default

静态文件托管

编辑默认站点配置文件,配置静态文件托管:

nginx 复制代码
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html; # 静态文件存放路径
    index index.html index.htm;

    server_name _;

    location / {
        try_files $uri $uri/ =404;
    }
}

4. 重启 NGINX

bash 复制代码
sudo systemctl restart nginx

常见用例

反向代理

配置NGINX作为反向代理服务器,将请求转发至后端应用:

nginx 复制代码
server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://backend_server;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        # 其他反向代理配置
    }
}
负载均衡

实现负载均衡配置:

nginx 复制代码
upstream backend {
    server backend1.example.com;
    server backend2.example.com;
    # 添加更多后端服务器
}

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://backend;
        # 负载均衡配置
    }
}
HTTPS 配置

为站点启用 HTTPS:

nginx 复制代码
server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /path/to/certificate.crt;
    ssl_certificate_key /path/to/private.key;

    location / {
        # HTTPS 配置
    }
}
相关推荐
刺客xs6 分钟前
linux GDB调试器
linux·运维·windows
板鸭〈小号〉1 小时前
connect 的断线重连
运维·服务器
黄昏晓x2 小时前
Linux----权限
linux·运维·服务器
小白不想白a2 小时前
【shell】每日shell练习(系统服务状态监控/系统性能瓶颈分析)
linux·运维·服务器
一匹电信狗2 小时前
【MySQL】数据库的相关操作
linux·运维·服务器·数据库·mysql·ubuntu·小程序
迦蓝叶2 小时前
JAiRouter v1.0.0 正式发布:企业级 AI 服务网关的开源解决方案
java·运维·人工智能·网关·spring·ai·开源
bugtraq20213 小时前
为什么.NET的System.IO.Compression无法解压zlib流
linux·运维·服务器
insight^tkk4 小时前
【Docker】记录一次使用docker部署dify网段冲突的问题
运维·人工智能·docker·ai·容器
K_i1344 小时前
Hadoop 集群自动化运维实战
运维·hadoop·自动化
Siren_dream5 小时前
在VMware上运行Linux(我选择的是Ubuntu)
linux·运维·ubuntu