Nginx简介

Nginx是高性能的HTTP和反向代理服务器,以其稳定性、丰富的功能和低资源消耗而著称。

主要配置文件

  • /etc/nginx/nginx.conf - 主配置文件
  • /etc/nginx/conf.d/ - 站点配置目录
  • /var/log/nginx/ - 日志目录

基本站点配置

server {
    listen 80;
    server_name www.example.com;
    root /var/www/html;
    index index.html index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

常用配置指令

指令作用
listen监听端口
server_name域名
root网站根目录
index默认首页
location路径匹配规则
proxy_pass反向代理

HTTPS配置

server {
    listen 443 ssl;
    server_name www.example.com;
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;
}

常用命令

  • nginx -t - 测试配置文件
  • nginx -s reload - 重载配置
  • nginx -s stop - 停止服务