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 配置
    }
}
相关推荐
虎头金猫3 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
还是大剑师兰特3 天前
解决nginx错误:http://localhost:7000正常,http://localhost:7000/map 报错404
nginx·大剑师
AI职业加油站3 天前
AI智能体应用工程师证书:政策红利下的职业新风口
大数据·运维·人工智能·学习·职场发展
此冬歌咏3 天前
K8s 节点故障实战:优雅驱逐 31 秒,硬故障 331 秒,以及那个永远 Pending 的 Pod
运维·k8s
xing-xing3 天前
Docker容器中Nginx站点根目录网页配置访问
nginx·docker
-梅3 天前
linux(8) 软硬链接
linux·运维·服务器
张洛闻Eren3 天前
k8s云原生【第十课】:水平 Pod 自动扩缩容
运维·数据库·云原生·kubernetes·github
其实防守也摸鱼3 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
布裘4 天前
【银河麒麟】V4桌面图标消失,右击鼠标没反应排查
运维·银河麒麟·桌面环境
懂软件的胡子个哥4 天前
微信机器人为什么需要“回复候选”而不是所有 AI 内容直接发送
运维·微信·自动化·wechatapi·个人微信号二次开发