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 配置
    }
}
相关推荐
档案宝档案管理2 小时前
权限分级管控,全程可追溯,筑牢会计档案安全防线
运维·网络·人工智能
倔强的石头1063 小时前
【Linux指南】基础IO系列(八):实战衔接 —— 给微型 Shell 添加完整重定向功能
linux·运维·服务器
观北海4 小时前
AiScan-N:AI全自动化渗透测试工具的深度技术解析
运维·自动化
Ujimatsu4 小时前
虚拟机安装Ubuntu 26.04.x及其常用软件(2026.4)
linux·运维·ubuntu
Agent产品评测局7 小时前
制造业生产调度自动化落地,完整步骤与避坑指南:2026企业级智能体选型与实战全景
运维·人工智能·ai·chatgpt·自动化
狂奔的sherry7 小时前
一次由 mount 引发的 Linux 文件系统“错觉”
linux·运维·服务器
志栋智能7 小时前
超自动化巡检:让合规与审计变得轻松简单
运维·网络·人工智能·自动化
小黑要努力7 小时前
智能音箱遇到的问题(一)
linux·运维·git
好度7 小时前
自动化教程-封装浏览器驱动
运维·自动化
ch3nyuyu7 小时前
静态库和动态库的制作
linux·运维·开发语言