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 配置
    }
}
相关推荐
ljh57464911921 分钟前
linux awk 命令
linux·运维·chrome
我是谁??35 分钟前
Rocky9+ Docker + 容器内Linux桌面环境 + Web远程
运维·docker·容器
阿虎儿36 分钟前
Ubuntu 如何开启 Root 用户 SSH 登录
运维
the sun341 小时前
Linux驱动开发:环境准备与报错处理
linux·运维·服务器
彭泽布衣1 小时前
Linux如何指定源端口打流
linux·运维·网络
Ciel_75211 小时前
OpenClaw 深度进阶:记忆系统、多智能体架构与自动化调度全解析
运维·自动化
晨晖21 小时前
Linux命令3
linux·运维·服务器
素雨迁喜2 小时前
Linux平台下git工具的使用
linux·运维·git
bing_feilong2 小时前
Ubuntu Tips
linux·运维
SeanDe2 小时前
【Linux `top` 命令详解(结合截图逐行拆解)】
linux·运维·服务器