nginx安装启动配置
一、结构(linux)
Linux上安装nginx和windows上安装不同,windows上解压配置就能使用,linux复杂一些
下图为nginx安装好的结构:
nginx ||这是最终的使用目录,静态资源放在这个文件夹中
nginx-1.20.0 ||这是源代码目录,新增模块需要重新编译
nginx-1.20.0.tar.gz ||这是压缩文件
nginx的默认安装路径在’/usr/local’下,如果修改需要在安装时特别指定
二、安装(linux)
1.系统
新的linux环境,基本什么都没装
2.下载解压安装
//切换到local目录,nginx文件夹会默认放到这个目录下
cd /usr/local
//下载安装包
wget https://nginx.org/download/nginx-1.20.0.tar.gz
//报错:-bash: wget: command not found,没有这个命令
sudo yum install wget
//重新运行
wget https://nginx.org/download/nginx-1.20.0.tar.gz
//解压安装包
tar zxvf nginx-1.20.0.tar.gz
//切换到源码目录
cd nginx-1.20.0
//安装和编译源码
./configure #输入后回车
//若报错:./configure: error: C compiler cc is not found
sudo yum groupinstall "Development Tools"
//若报错:./configure: error: the HTTP rewrite module requires the PCRE library.
sudo yum install pcre pcre-devel
//若报错./configure: error: the HTTP gzip module requires the zlib library.
sudo yum install zlib-devel
//重新执行
./configure
make #输入后回车
sudo make install #输入后回车
//常用命令
//首先切换到sbin目录
cd /usr/local/nginx/sbin
//运行
./nginx
//停止
./nginx -s stop
//重启
./nginx -s reload
//查看启动进程
ps -ef | grep nginx
输出:
root 8536 1 0 15:17 ? 00:00:00 nginx: master process ./nginx
nobody 8537 8536 0 15:17 ? 00:00:00 nginx: worker process
root 8563 32735 0 15:18 pts/1 00:00:00 grep --color=auto nginx
8536 是 NGINX 的主进程 ID(PID),它负责管理所有 NGINX 工作进程。
8537 是 NGINX 的工作进程的 PID,它处理实际的客户端请求。
3.如果出错,卸载重装
//首先切换到sbin目录,停止正在运行的服务
cd /usr/local/nginx/sbin
./nginx -s stop
//切换到源码目录进行卸载
sudo make uninstall
//删除相关文件
sudo rm -rf /usr/local/nginx
//删除配置文件
sudo rm -rf /etc/nginx
至此卸载完成可以重新安装
4.使用 systemd管理 NGINX 服务
首先有一个问题,为什么有些博客在写到启动nginx使用’sudo systemctl start nginx’命令,但是直接在xshell中使用这个命令,报错’Failed to start nginx.service: Unit not found.',为什么这个命令是没有运行成功的?
因为’sudo systemctl start nginx’需要nginx与系统服务管理器进行集成后才能使用。./nginx 命令运行的是一个独立的 NGINX 进程,但是,正常情况下,应该使用 systemd 来启动和管理 NGINX 服务。这样可以确保 NGINX 在系统启动时自启,并能够通过 systemctl 命令进行管理和监控。
//首先切换到sbin目录,停止正在运行的服务
cd /usr/local/nginx/sbin
./nginx -s stop
//查看进程,确保服务停止
ps -ef | grep nginx
//创建新文件
sudo vi /etc/systemd/system/nginx.service
//按下i键进入编辑,复制如下内容到新文件后,按下esc键退出,输入':wq'保存
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
//重新加载 systemd 配置文件
sudo systemctl daemon-reload
//启动 NGINX 服务
sudo systemctl start nginx
//停止 NGINX 服务
sudo systemctl stop nginx
//重启 NGINX 服务
sudo systemctl restart nginx
//检查 NGINX 服务是否正在运行
sudo systemctl status nginx
//查看 NGINX 服务的详细日志
sudo journalctl -u nginx
5.监测和加密
不需要加密访问忽略此部分
//切换目录
cd /usr/local/nginx-1.20.0
//添加模块
./configure --with-http_stub_status_module --with-http_ssl_module
//若安装失败报错如下:
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
这需要我们安装 OpenSSL 库:
#对于 Debian/Ubuntu 系统,可以使用:
sudo apt-get update
sudo apt-get install libssl-dev
#对于 Red Hat/CentOS 系统,可以使用:
sudo yum install openssl-devel
#对于 Fedora 系统,可以使用:
sudo dnf install openssl-devel
#对于 Arch Linux 系统,可以使用:
sudo pacman -S openssl
最后进行重新编译:
//重新编译
make
make install
其中,'–with-http_stub_status_module’表示启用 HTTP stub status 模块。HTTP stub status 模块提供了一个简单的页面,用于监控 NGINX 服务器的运行状态。该页面显示了当前连接数、请求处理信息和其他有用的统计数据。
访问监测页面需要添加如下配置:
//切换到nginx.conf目录
cd /usr/local/nginx/conf
//编辑配置文件,在某一个server{}中添加如下配置
location /nginx_status {
stub_status;
allow 127.0.0.1; # 允许访问的 IP 地址
# deny all; # 去除注释则禁止其他 IP 地址访问
}
访问地址为:http://nginx所在的ip:server的端口/nginx_status,返回如下:
server accepts handled requests: 表示服务器已接受并处理了 2 个请求。
Reading: 0: 表示当前没有正在读取的连接。
Writing: 1: 表示有 1 个正在写入的连接。
Waiting: 1: 表示有 1 个连接正在等待。
'with-http_ssl_module’表示启用 HTTP SSL 模块的 HTTPS 页面的访问,需要在 NGINX 配置文件中配置SSL 证书和密钥,配置完成后可以使用https://your-domain来访问服务。
//放开文件末尾的注释并修改
server {
listen 443 ssl;
server_name localhost; # 替换为你的实际域名
ssl_certificate /usr/local/nginx/conf/cert.pem; # 替换为 SSL 证书的路径
ssl_certificate_key /usr/local/nginx/conf/cert.key; # 替换为 SSL 证书密钥的路径
# 其他配置项...
ssl_session_cache shared:SSL:1m;#指定共享的 SSL 会话缓存,大小为 1MB。
ssl_session_timeout 5m;#指定 SSL 会话的超时时间为 5 分钟。在此时间后,会话将被丢弃。
ssl_ciphers HIGH:!aNULL:!MD5;#指定使用的 SSL 加密算法。这个配置将只允许使用高级加密算法,同时禁用了空加密和 MD5。
ssl_prefer_server_ciphers on;#指定服务端加密算法优先级。当启用该选项时,服务器将优先选择自己支持的加密算法。
location / {
root html;
index index.html index.htm;
}
}
SSL/TLS证书需要去证书颁发机构申请。
三、安装(windows)
华为云下载nginx,选择一个zip格式的下载,例如’nginx-1.22.1.zip‘,下载完成后可以放到D盘,然后解压。
解压目录如下:
双击nginx.exe即可启动nginx,打开任务管理器,可以看到如下后台进程
浏览器地址栏输入localhost即可访问nginx的默认界面
四、配置(通用)
nginx的配置文件是conf文件夹中的nginx.conf文件,Linux上的该文件可以通过xftp传到本地,然后编辑修改。
4.1原始配置文件详解
#指定运行Nginx进程的用户,默认情况下是nobody用户
#user nobody;
#指定Nginx启动的工作进程数,可以根据服务器的硬件配置和负载情况进行调整
worker_processes 1;
#用于配置错误日志的位置和详细程度。第一行指定了错误日志文件的路径,后面两行可选
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#指定了Nginx进程ID文件的位置。默认情况下,Nginx将该文件保存在logs/nginx.pid中
#pid logs/nginx.pid;
#配置Nginx的事件模型的部分。worker_connections参数指定了每个工作进程允许的最大并发连接数。
events {
worker_connections 1024;
}
http {
#用于引入其他文件的内容。mime.types文件定义了MIME类型与文件扩展名之间的映射关系。
#通过包含这个文件,Nginx 可以根据请求的文件扩展名设置适当的 MIME 类型。
include mime.types;
#用于设置默认的MIME类型。在这里,表示不知道具体 MIME 类型的文件将被视为二进制流文件。
default_type application/octet-stream;
#用于定义日志格式。定义了一个名为main的日志格式,每个变量都会被实际的值替换。
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#用于设置访问日志的位置和日志格式。将访问日志写入到logs/access.log 文件中,并使用main格式。
#access_log logs/access.log main;
#用于启用或禁用使用 sendfile()系统调用来传输文件。使用 sendfile()能加速文件传输。
sendfile on;
#tcp_nopush on;
#用于设置keep-alive连接的超时时间。
#keepalive_timeout 0;
keepalive_timeout 65;
#控制开启压缩
#gzip on;
server {
#监听80端口
listen 80;
#用于设置服务器的域名或IP
server_name localhost;
#用于设置字符编码
#charset koi8-r;
#设置日志
#access_log logs/host.access.log main;
#location块用于匹配特定的URL路径,并为其指定相应的配置。路径以/开头时,将使用root指令指定的目录html
#作为根目录。并且,当请求路径没有指定具体的文件名时,会默认返回 index.html或index.htm。
location / {
root html;
index index.html index.htm;
}
#错误页面配置
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#用于设置自定义的错误页面。将HTTP状态码为500、502、503和504的错误页重定向到/50x.html 页面。此外,
#location 块匹配到 /50x.html 路径时,使用 root 指令指定的目录html作为根目录。
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#当请求路径以 .php 结尾时,会将请求代理到 http://127.0.0.1,即本地的 Apache 服务器,默认监听80端口。
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#当请求路径以 .php 结尾时,会将请求转发到 FastCGI 进程,即在 127.0.0.1 地址上监听端口号 9000 的进程。
#同时,root指令指定根目录html,fastcgi_index 指令指定了默认的索引文件名为 index.php,
#fastcgi_param 指令指定了传递给 FastCGI 进程的参数(这里设置了 SCRIPT_FILENAME 参数),i
#nclude 指令包含了 FastCGI 配置文件。
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#使用正则表达式匹配所有以.ht 开头的文件或文件夹,然后使用 deny 指令拒绝所有请求。这样做是为了保护服务器的敏感文件。
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#同时监听 8000 和 somename:8080 这两个端口
# listen 8000;
# listen somename:8080;
#服务器名为 somename,同时还有别名 alias 和 another.alias
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
#第二部分的监测加密已讲过
#HTTPS server
#server {
# listen 443 ssl;
# server_name localhost;
......
......
#}
}
4.2简易版
#找到gzip on行,添加如下内容
gzip on;
gzip_min_length 1k;
gzip_comp_level 9;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
#找到第一个server,修改配置
#如果有多个监听的端口需要配置多个server
server {
#修改端口号
listen 80;
#如果使用域名将localhots改为域名,没有域名可以不修改
server_name localhost;
#如果有多个不同路径的请求需要配置多个location,遵循最长匹配原则
#编译后的静态资源放在遇conf文件夹同级的html中
location / {
root html;
index index.html index.htm;
}
#在linux中配置,路径从根目录开始
#匹配以 /system1 开头的URL路径
location /system1{
#指定/usr/local/nginx/system1作为实际文件系统路径
alias /usr/local/nginx/system1;
index index.html index.htm;
}
#在windows中配置,路径从根目录开始
#匹配以 /system2 开头的URL路径
location /system2 {
#指定D:/nginx-1.22.1/system2作为实际文件系统路径
alias D:/nginx-1.22.1/system2 ;
index index.html index.htm;
#如果刷新后404可以加以下配置
try_files $uri $uri/ /system2/index.html;
}
}
五、异常
1.403 forbidden
#确认访问路径是否有二级域名
#若没有二级域名
server {
listen 80;
server_name localhost;
location / {
root your_project;
index index.html index.htm;
}
}
#将your_project替换为实际项目名称
#没有二级域名,必须要有这个默认项
2.404 not found
到nginx/logs目录下跟踪日志,调整项目位置。