豌豆Ai站群搜索引擎系统 V.25.10.25 网址:www.wd.chat

🗣 Wd提问: nginx完整配置

🤖 Ai回答:
以下是一个完整的Nginx配置示例,适用于一个典型的Web服务器场景。该配置文件包括了基本的HTTP服务、SSL/TLS支持、反向代理、负载均衡等功能。你可以根据实际需求进行修改和扩展。

Nginx 配置文件结构
Nginx 的主配置文件通常位于 `/etc/nginx/nginx.conf`,而虚拟主机配置文件通常位于 `/etc/nginx/conf.d/` 或 `/etc/nginx/sites-available/` 目录下。

1、主配置文件 (`nginx.conf`)

nginx
/etc/nginx/nginx.conf

user nginx; 运行Nginx的用户
worker_processes auto; 自动根据CPU核心数设置工作进程数

error_log /var/log/nginx/error.log warn; 错误日志路径和级别
pid /var/run/nginx.pid; Nginx进程ID文件

events {
worker_connections 1024; 每个工作进程的最大连接数
}

http {
include /etc/nginx/mime.types; 包含MIME类型定义
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 /var/log/nginx/access.log main; 访问日志格式

sendfile on; 启用sendfile以提高文件传输效率
tcp_nopush on; 启用TCP_NOPUSH选项
tcp_nodelay on; 启用TCP_NODELAY选项
keepalive_timeout 65; 连接保持超时时间
types_hash_max_size 2048; 类型哈希表的最大大小

include /etc/nginx/conf.d/*.conf; 包含其他配置文件

Gzip压缩设置
gzip on;
gzip_disable msie6;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

SSL相关设置
ssl_protocols TLSv1.2 TLSv1.3; 只启用TLS 1.2和1.3
ssl_prefer_server_ciphers on;
ssl_ciphers HIGH:!aNULL:!MD5;

默认服务器配置
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;

return 444; 关闭默认服务器,返回444状态码(无响应)
}

包含虚拟主机配置
include /etc/nginx/sites-enabled/*;
}

2、虚拟主机配置文件示例 (`/etc/nginx/sites-available/65.hk`)

nginx
/etc/nginx/sites-available/65.hk

server {
listen 80;
listen [::]:80;

server_name 65.hk www.65.hk;

HTTP重定向到HTTPS
return 301 https://$host$request_uri;
}

server {
listen 443 ssl http2;
listen [::]:443 ssl http2;

server_name 65.hk www.65.hk;

SSL证书和密钥
ssl_certificate /etc/letsencrypt/live/65.hk/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/65.hk/privkey.pem;

设置根目录
root /var/www/65.hk/html;
index index.html index.htm index.php;

访问日志和错误日志
access_log /var/log/nginx/65.hk.access.log;
error_log /var/log/nginx/65.hk.error.log;

处理静态文件
location / {
try_files $uri $uri/ =404;
}

PHP-FPM处理PHP文件
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; 根据你的PHP版本调整
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

防止访问隐藏文件
location ~ /\.ht {
deny all;
}

反向代理配置示例
location /app/ {
proxy_pass http://localhost:8080; 将请求转发到后端应用
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

3、负载均衡配置示例

如果你有多个后端服务器,可以使用Nginx的负载均衡功能:

nginx
/etc/nginx/conf.d/load_balancer.conf

upstream backend {
server backend1.65.hk weight=5; 权重为5
server backend2.65.hk;
server backend3.65.hk backup; 备份服务器
}

server {
listen 80;

server_name loadbalancer.65.hk;

location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

4、其他常用配置

4.1、缓存配置

nginx
/etc/nginx/conf.d/cache.conf

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

server {
listen 80;
server_name cache.65.hk;

location / {
proxy_cache my_cache;
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

add_header X-Proxy-Cache $upstream_cache_status; 返回缓存状态
}
}

4.2、WebSocket 支持

nginx
location /ws/ {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

5、测试配置并重启Nginx

在修改完配置文件后,务必测试配置文件的语法是否正确:

bash
sudo nginx -t

如果测试通过,重新加载或重启Nginx服务:

bash
sudo systemctl reload nginx
或者
sudo systemctl restart nginx

总结

这个配置文件涵盖了Nginx的基本功能,包括HTTP、HTTPS、反向代理、负载均衡、缓存等。你可以根据自己的需求进一步扩展和优化配置。

Ai作答

📣 商家广告


公司

广告招商

高防vps

日付VPS

起名


0

IP地址: 235.245.10.79

搜索次数: 0

提问时间: 2025-11-04 13:16:00

🛒 域名购买
nf.gs
sktq.cn
862.net.cn
352.net.cn
191.net.cn
287.net.cn
67.plus
wd.chat
wz.chat
z-u.net

❓️ 热门提问
目前好用的搜索引擎
成都 vps
云主机,云服务器哪里的比较好
云主机多网站吗
甲骨文vps申请
如何往vps上传东西
服务器托管 排行
柠檬如何保存更久
解析域名
vps 主机 推荐
豌豆Ai站群搜索引擎系统

🌐 域名评估
kmpe.cn
kr.gs
evtol.qh.cn
163.com
mmmf.com.cn
dji.zj.cn
uav.jx.cn
yg.com.cn
ftp.ink
oppo.he.cn

⛏ 最新挖掘
凯里酸汤鱼
贵阳烤脑花
都匀毛尖茶
花溪牛肉粉
贵阳烤生蚝
贵阳烤豆腐
贵阳纸包鱼
贵阳特色菜

🖌 热门作画

🤝 关于我们:
豌豆Ai 域名 建站 站群 留痕 推广 评估 源码
开发Ai 工具 日记 价格 加盟 广告 流量 留言 联系

🗨 加入群聊
群

🔗 友情链接
聊天室网页版  谷歌关键词优化  ai提问

🧰 站长工具
Ai工具  whois查询  搜索

📢 温馨提示:本站所有问答由Ai自动创作,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。

👉 技术支持:本站由豌豆Ai提供技术支持,使用的最新版:《豌豆Ai站群搜索引擎系统 V.25.10.25》搭建本站。

上一篇 60021 60022 60023 下一篇