Web技术与Nginx网站环境部署笔记

年夜雪

关注

阅读 15

04-28 21:00

一、Web技术基础

1. HTTP协议基础

  • 请求方法:GET/POST/PUT/DELETE/HEAD
  • 状态码
  • 200 OK
  • 301/302 重定向
  • 404 未找到
  • 500 服务器错误
  • 请求/响应头
  • Content-Type
  • User-Agent
  • Cookie/Set-Cookie
  • Cache-Control

2. Web架构组成

  • 前端技术栈:HTML5/CSS3/JavaScript
  • 后端技术栈:PHP/Python/Java/Node.js
  • 数据库:MySQL/MongoDB/Redis
  • Web服务器:Nginx/Apache

二、Nginx基础

1. Nginx特点

  • 高性能事件驱动架构
  • 低内存消耗
  • 高并发处理能力
  • 反向代理/负载均衡
  • 静态内容高效处理

2. 安装Nginx


# Ubuntu/Debian
sudo apt update
sudo apt install nginx

# CentOS/RHEL
sudo yum install epel-release
sudo yum install nginx

# 启动/停止/重启
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx

# 开机自启
sudo systemctl enable nginx

三、Nginx核心配置

1. 配置文件结构


/etc/nginx/
├── nginx.conf          # 主配置文件
├── conf.d/             # 额外配置目录
├── sites-available/    # 可用站点配置
└── sites-enabled/      # 已启用站点配置(符号链接)

2. 基础配置示例


# /etc/nginx/nginx.conf 核心部分
user www-data;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    
    access_log /var/log/nginx/access.log;
    
    sendfile on;
    keepalive_timeout 65;
    
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

3. 虚拟主机配置


# /etc/nginx/sites-available/example.com
server {
    listen 80;
    server_name example.com www.example.com;
    
    root /var/www/example.com/html;
    index index.html index.htm;
    
    location / {
        try_files $uri $uri/ =404;
    }
    
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }
    
    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;
}

4. 启用站点配置


# 创建符号链接
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

# 测试配置并重载
sudo nginx -t
sudo systemctl reload nginx

四、Nginx高级配置

1. HTTPS配置(Let's Encrypt)


# 安装Certbot
sudo apt install certbot python3-certbot-nginx

# 获取证书
sudo certbot --nginx -d example.com -d www.example.com

# 自动续期测试
sudo certbot renew --dry-run

2. 负载均衡配置


upstream backend {
    server backend1.example.com weight=5;
    server backend2.example.com;
    server backup.example.com backup;
}

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

3. 缓存配置


proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m;

server {
    location / {
        proxy_cache my_cache;
        proxy_pass http://backend;
        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 404 1m;
    }
}

五、PHP环境配置

1. 安装PHP-FPM


# Ubuntu 20.04
sudo apt install php7.4-fpm php7.4-mysql php7.4-curl php7.4-gd

# 检查状态
sudo systemctl status php7.4-fpm

2. Nginx与PHP集成


location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

六、Python环境部署

1. uWSGI配置


[uwsgi]
module = wsgi:app
master = true
processes = 5
socket = myapp.sock
chmod-socket = 660
vacuum = true
die-on-term = true

2. Nginx与uWSGI集成


server {
    location / {
        include uwsgi_params;
        uwsgi_pass unix:/path/to/myapp.sock;
    }
}

七、安全加固

1. 基础安全配置


# 禁用server tokens
server_tokens off;

# 限制HTTP方法
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
    return 405;
}

# 防止点击劫持
add_header X-Frame-Options "SAMEORIGIN";

# XSS防护
add_header X-XSS-Protection "1; mode=block";

2. 访问控制


# IP限制
location /admin {
    allow 192.168.1.0/24;
    deny all;
}

# 基础认证
location /secure {
    auth_basic "Restricted Area";
    auth_basic_user_file /etc/nginx/.htpasswd;
}

八、性能优化

1. 静态资源优化


location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 365d;
    add_header Cache-Control "public, no-transform";
}

# 开启gzip压缩
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml;

2. 连接优化


# 调整缓冲区大小
client_body_buffer_size 10K;
client_header_buffer_size 1k;
client_max_body_size 8m;
large_client_header_buffers 4 16k;

# 超时设置
client_body_timeout 12;
client_header_timeout 12;
keepalive_timeout 15;
send_timeout 10;

九、日志管理

1. 访问日志格式


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 /var/log/nginx/access.log main;

2. 日志轮转


# /etc/logrotate.d/nginx
/var/log/nginx/*.log {
    daily
    missingok
    rotate 14
    compress
    delaycompress
    notifempty
    create 0640 www-data adm
    sharedscripts
    postrotate
        /usr/sbin/nginx -s reload
    endscript
}

十、常见问题排查

1. 检查配置语法


sudo nginx -t

2. 查看错误日志


tail -f /var/log/nginx/error.log

3. 检查端口监听


sudo netstat -tulnp | grep nginx

4. 检查进程状态


ps aux | grep nginx

精彩评论(0)

0 0 举报