Nginx

一、Nginx

Nginx(Engine X) 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 邮件代理服务器。它以稳定性高、占用内存少、并发能力强(官方测试可支撑 5 万并发连接)而著称。

常见用途:

  • 作为 Web 服务器(静态资源)

  • 作为反向代理服务器

  • 作为负载均衡器

  • 提供 HTTPS(TLS/SSL)

  • 提供缓存加速

  • 接入 WAF、防盗链、防DDoS 保护等功能"

二、Nginx 安装方式

1. 安装依赖

yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

2. 下载并编译安装

cd /usr/local/src  ##随意目录
wget http://nginx.org/download/nginx-1.24.0.tar.gz  ##yum -y install wget
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
./configure --prefix=/usr/local/nginx
make && make install

3. 启动 Nginx

/usr/local/nginx/sbin/nginx

或者下面

##添加 systemd 启动服务:
vim /usr/lib/systemd/system/nginx.service


#
[Unit]
Description=nginx
After=network.target

[Service]
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
Restart=always

[Install]
WantedBy=multi-user.target

##启动并设置开机启动:
systemctl daemon-reexec
systemctl enable nginx
systemctl start nginx

4. 常用命令

nginx -t                # 测试配置是否正确
nginx -s reload         # 重新加载配置
nginx -s stop           # 停止服务
nginx -v                # 查看版本

5.其他方式

1、配置yum

cd  /etc/yum.repos.d/

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/
$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true


yum -y install nginx

三、Nginx目录结构

/etc/nginx/                         nginx各种配的目录
/etc/nginx/nginx.conf                   主配置文件
/etc/nginx/conf.d/                子配置文件(网站)
/etc/nginx/conf.d/default.conf     默认的子配置文件
/usr/sbin/nginx                nginx命令
/usr/share/nginx/html/          nginx默认的站点目录,网站的根目录
/var/log/nginx/                nginx日志: 访问日志,错误日志 ,跳转日志

1.核心配置文件

##默认配置文件路径:
/usr/local/nginx/conf/nginx.conf

##配置结构:
# 全局块
worker_processes  1;

# 事件块
events {
    worker_connections  1024;
}

# HTTP块
http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    # 日志配置
    access_log  logs/access.log;

    # 虚拟主机配置
    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page  404              /404.html;
    }
}

四、常用功能场景配置

1. 静态资源 Web 服务

server {
    listen 80;
    server_name www.example.com;

    location / {
        root /var/www/html;
        index index.html index.htm;
    }
}

2. 反向代理

server {
    listen 80;
    server_name app.example.com;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

3. 负载均衡

http {
    upstream backend {
        server 192.168.1.10:8080;
        server 192.168.1.11:8080;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://backend;
        }
    }
}

支持多种调度算法:

  • 轮询(默认)server ip:port;

  • 权重server ip:port weight=3;

  • IP哈希ip_hash;

 4. HTTPS 配置(使用 SSL)

server {
    listen 443 ssl;
    server_name www.example.com;

    ssl_certificate     /etc/nginx/cert.pem;
    ssl_certificate_key /etc/nginx/cert.key;

    location / {
        root /var/www/html;
    }
}

5. URL 重写(rewrite)

location /old {
    rewrite ^/old(.*)$ /new$1 permanent;
}

6. 配合 PHP 使用(FastCGI)

server {
    listen 80;
    server_name www.example.com;

    location / {
        root /var/www/html;
        index index.php index.html index.htm;
    }

    location ~ \.php$ {
        root /var/www/html;
        fastcgi_pass 127.0.0.1:9000;  # PHP-FPM监听端口
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

六、日志分析

## Access Log 格式配置
    
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"';

access_log logs/access.log main;

##查看访问日志:
tail -f /usr/local/nginx/logs/access.log

五、Nginx 配置文件解释

# 全局配置区域(影响所有 worker 进程)
user  nginx;                    # 指定运行 Nginx 的系统用户(建议设为nginx)
worker_processes  1;           # 启动的工作进程数,通常等于CPU核心数
error_log  logs/error.log;     # 错误日志路径
pid        logs/nginx.pid;     # 存储主进程 PID 的文件路径
# 事件(events)块
events {
    worker_connections  1024;  # 每个 worker 最多同时处理的连接数
    use epoll;                # 使用 epoll 模型(Linux 高性能事件驱动)
    multi_accept on;          # 一个事件循环中尽可能多地接受新连接
}
# HTTP 配置块,所有 HTTP 服务相关配置放这里
http {
    include       mime.types;                  # 引入文件类型映射表
    default_type  application/octet-stream;   # 默认文件类型(二进制流)

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';  # 自定义访问日志格式

    access_log  logs/access.log  main;         # 设置访问日志存储路径和格式

    sendfile        on;                        # 启用高效文件传输 sendfile
    tcp_nopush      on;                        # 减少网络包数量,提升效率
    tcp_nodelay     on;                        # 禁用 Nagle 算法,加快小数据包发送速度
    keepalive_timeout  65;                     # 设置连接保持时间(秒)
    types_hash_max_size 2048;                  # MIME类型哈希表最大大小

    server_tokens off;                         # 不在 header 中显示 Nginx 版本信息(安全)

    # Gzip 压缩设置(提升传输效率)
    gzip  on;                                  # 开启 Gzip 压缩
    gzip_disable "msie6";                      # 关闭对 IE6 的 Gzip 支持
    gzip_types text/plain application/xml;     # 设置 Gzip 支持的 MIME 类型

    # 引入虚拟主机配置
    include /usr/local/nginx/conf/conf.d/*.conf;
}

虚拟主机配置(server 块)

server {
    listen       80;                          # 监听的端口
    server_name  www.example.com;             # 绑定的域名

    # 访问日志配置
    access_log  logs/example.access.log  main;

    # 默认访问根目录配置
    location / {
        root   /var/www/html;                 # 指定站点根目录
        index  index.html index.htm;          # 默认首页文件
    }

    # 错误页面设置
    error_page  404              /404.html;   # 定制 404 页面
    location = /404.html {
        root /var/www/html;
    }

    # 拦截 .php 请求,交给 PHP-FPM
    location ~ \.php$ {
        root           /var/www/html;
        fastcgi_pass   127.0.0.1:9000;         # PHP-FPM监听端口
        fastcgi_index  index.php;
        include        fastcgi_params;         # 引入 FastCGI 参数
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    }

    # 拒绝访问 .htxxx 文件
    location ~ /\.ht {
        deny all;
    }
}

参数说明表

参数名含义
user设置运行 Nginx 的系统用户
worker_processes设置工作进程数(建议为CPU核心数)
error_log设置错误日志路径
pid设置 PID 文件路径
worker_connections每个进程最大连接数
use epoll使用 epoll 事件驱动模型(高并发推荐)
include引入外部配置文件
default_type默认文件类型
sendfile使用 sendfile 系统调用传输文件,提升性能
keepalive_timeoutHTTP keep-alive 超时时间
gzip启用 gzip 压缩
server定义一个虚拟主机块
listen设置监听端口
server_name设置响应的域名
location设置 URI 匹配和处理规则
proxy_pass设置反向代理地址
root设置请求资源的根目录
index设置默认首页
access_log设置访问日志路径及格式
error_page自定义错误页面
deny all拒绝所有访问(安全策略)

配置目录结构

/usr/local/nginx/
├── conf/
│   ├── nginx.conf             # 主配置文件
│   ├── mime.types             # 文件类型映射表
│   └── conf.d/                # 虚拟主机/子配置
│       ├── default.conf
│       ├── ssl.conf
│       └── app1.conf
├── logs/
│   ├── access.log
│   └── error.log
└── html/                      # 默认网页根目录
    └── index.html

####有待补充。。。。。。

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值