介绍
安装
1、更新软件包列表
sudo apt update
2、安装 Nginx
sudo apt install nginx -y
验证安装并管理服务
检查服务状态:运行以下命令,确保服务状态显示为 active (running)。
sudo systemctl status nginx
管理 Nginx 服务
使用 systemctl 命令来控制Nginx服务的生命周期。
1、启动 Nginx 服务
sudo systemctl start nginx
2、停止 Nginx 服务
sudo systemctl stop nginx
3、重启 Nginx 服务
sudo systemctl restart nginx # 重启 Nginx 服务
4、重载 Nginx 配置,服务不中断
sudo systemctl reload nginx
5、设置 Nginx 开机自启
sudo systemctl enable nginx
6、取消 Nginx 开机自启
sudo systemctl disable nginx
7、查看 IP
ip add
8、访问 ip
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
查看配置
路径
cd /etc/nginx
文件目录
/etc/nginx/
├── conf.d/ # 通用配置片段目录
├── sites-available/ # 可用站点配置(源文件)
├── sites-enabled/ # 已启用站点配置(软链接)
├── modules-available/ # 可用模块配置
├── modules-enabled/ # 已启用模块配置
├── snippets/ # 可复用的配置片段
├── nginx.conf # 主配置文件
├── fastcgi.conf # FastCGI配置(完整版)
├── fastcgi_params # FastCGI参数配置(基础版)
├── scgi_params # SCGI参数配置
├── uwsgi_params # uWSGI参数配置
├── proxy_params # 反向代理参数配置
├── mime.types # MIME类型映射表
├── koi-utf # 字符集转换表(KOI8-R→UTF-8)
├── koi-win # 字符集转换表(KOI8-R→Windows-1251)
├── win-utf # 字符集转换表(Windows-1251→UTF-8)
└── koi-utf # 字符集转换文件
nginx.conf 配置文件
# 设置运行Nginx工作进程的系统用户
# www-data 是大多数Linux发行版(如Ubuntu/Debian)的默认Web用户
user www-data;
# worker_processes auto;
# 自动设置工作进程数量,通常等于CPU核心数,提高并发处理能力
worker_processes auto;
# worker_cpu_affinity auto;
# 自动将工作进程绑定到不同的CPU核心,提高缓存命中率和性能
worker_cpu_affinity auto;
# pid /run/nginx.pid;
# 指定Nginx主进程ID(PID)文件的存放位置
pid /run/nginx.pid;
# error_log /var/log/nginx/error.log;
# 错误日志文件路径,记录Nginx运行时的错误信息
error_log /var/log/nginx/error.log;
# include /etc/nginx/modules-enabled/*.conf;
# 包含所有启用的Nginx动态模块配置文件(如第三方模块)
include /etc/nginx/modules-enabled/*.conf;
# events 块:配置Nginx连接处理机制
events {
# worker_connections 768;
# 每个工作进程最大并发连接数,默认768
# 总并发数 = worker_processes × worker_connections
worker_connections 768;
# multi_accept on;
# (注释状态)允许工作进程一次性接受所有新连接,提高效率
# multi_accept on;
}
# http 块:Web服务器核心配置
http {
##
# 基础设置
##
# sendfile on;
# 启用高效文件传输模式(零拷贝),减少CPU和内存开销
sendfile on;
# tcp_nopush on;
# 与sendfile配合,在Linux上优化数据包发送,减少网络开销
tcp_nopush on;
# types_hash_max_size 2048;
# MIME类型哈希表大小,影响MIME类型匹配性能
types_hash_max_size 2048;
# server_tokens build;
# 错误页显示Nginx版本号和编译信息(建议生产环境设为off隐藏版本号)
server_tokens build; # 安全建议:生产环境应改为 off
# server_names_hash_bucket_size 64;
# 服务器域名哈希表大小(通常无需修改,除非有很多长域名)
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
# 重定向时使用请求中的域名而非配置文件中的server_name
# server_name_in_redirect off;
# include /etc/nginx/mime.types;
# 导入MIME类型定义文件,关联文件扩展名与Content-Type
include /etc/nginx/mime.types;
# default_type application/octet-stream;
# 默认MIME类型,无法识别文件类型时按二进制流处理
default_type application/octet-stream;
##
# SSL 安全设置
##
# ssl_protocols TLSv1.2 TLSv1.3;
# 仅启用TLS 1.2和1.3协议(禁用不安全的SSLv3、TLS 1.0/1.1)
ssl_protocols TLSv1.2 TLSv1.3;
# ssl_prefer_server_ciphers off;
# 关闭服务器端优先选择加密套件,让客户端选择(提高兼容性)
ssl_prefer_server_ciphers off; # 不强制使用服务器端密码套件顺序
##
# 日志记录设置
##
# access_log /var/log/nginx/access.log;
# 访问日志文件路径,记录所有HTTP请求信息
access_log /var/log/nginx/access.log;
##
# Gzip 压缩设置
##
# gzip on;
# 启用Gzip压缩,减少传输数据量,加快网页加载速度
gzip on;
# gzip_vary on;
# (注释状态)在响应头添加Vary: Accept-Encoding,适配代理缓存
# gzip_vary on;
# gzip_proxied any;
# (注释状态)对所有代理请求启用压缩
# gzip_proxied any;
# gzip_comp_level 6;
# (注释状态)压缩级别(1-9),值越大压缩率越高但消耗CPU越多
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# (注释状态)压缩缓冲区大小,16个8KB的缓冲区
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# (注释状态)最低HTTP版本要求,1.1及以上才压缩
# gzip_http_version 1.1;
# gzip_types text/plain text/css ...;
# (注释状态)指定需要压缩的MIME类型
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# 虚拟主机配置(网站配置)
##
# include /etc/nginx/conf.d/*.conf;
# 包含 conf.d 目录下所有 .conf 文件(通用配置)
include /etc/nginx/conf.d/*.conf;
# include /etc/nginx/sites-enabled/*;
# 包含 sites-enabled 目录下所有配置文件(站点配置)
# 通常 sites-available 存放配置源文件,sites-enabled 存放软链接
include /etc/nginx/sites-enabled/*;
}
# mail 块:邮件代理服务器配置(已被注释,未启用)
#mail {
# # 示例认证脚本地址
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # HTTP认证接口地址
# # pop3_capabilities "TOP" "USER";
# # POP3协议支持的命令
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
# # IMAP协议支持的功能
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END




暂无评论内容