nginx安装与配置
原创
©著作权归作者所有:来自51CTO博客作者luan_225的原创作品,请联系作者获取转载授权,否则将追究法律责任
源码安装
依赖的库:
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g-dev
sudo apt-get install openssl libssl-dev
- pcre 正则相关的库
- zlib -- gzip on
- ss l传输 http
下载源码: https://nginx.org/en/download.html 使用稳定版本
wget https://nginx.org/en/download.html
tar -xv nginx-1.22.0.tar.gz # 解压
# 安装程序时目录要带上版号
./config --prefix=/app/nginx1.22.0 --with-http_ssl_module
# 创建软连接
sudo ln -s /app/nginx-1.22.0 /app/nginx
# 修改已存在的软链按, 软件升级时用
sudo ln -s /app/nginx-1.22.0 /app/nginx
添加环境变量:
$ sudo vi /etc/profile
# 最后一行
PATH="/app/nginx/sbin:$PATH"
# 使用修改生效
$ source /etc/profile
# 检查是否成功
$ nginx -v
启动nginx需要root权限,而上面对PATH修改只是在当前用户(在wsl中测试是这个结果),所以 nginx可以执行, 但是 sudo nginx
会提示命令找不到..
使用官方的apt源
https://nginx.org/en/linux_packages.html#Ubuntu
查看编译参数:
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.22.0/debian/debuild-base/nginx-1.22.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'
控制nginx
sudo nginx
# 退出
sudo nginx -s quit
# 重新加载配置文件
sudo nginx -s reload
# 测试配置文件是否OK
sudo nginx -T
配置文件
路径: /etc/nginx/nginx.com (主配置文件)
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
第22行: include /etc/nginx/conf.d/*.conf;
conf.d/*.conf是虚拟主机配置文件(每个网站一个配置文件),
参数解释
default.con是一个虚拟主机配置示例:
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
- server_name -- _(使用ip) 或域名 www.hao123.com 或 localhost(只能在本机访问)
--------------核心模块-----------------------------------
# 工作进程的所有者
user nginx;
# 工作进程数量,给定一个具体的数字,如果是auto与主机cpu数是量相同
worker_processes auto;
# 错误文件 路径 等级;
error_log /var/log/nginx/error.log notice;
# nginx 主进程pid 存放的路径
pid /var/run/nginx.pid;
-------------事件驱动模块------------------------
events {
worker_connections 1024; # 一个worker 并发处理 1024 个请求
use epoll; # 默认就是epoll模型,不需要指定(只在linux下有效)
}
----------------HTTP网站配置------------------------------
http {
# 当中配置的类型,浏览器可以直接打开,如果没有点击就直接下载
include /etc/nginx/mime.types;
# 点击以后直接下载的类型
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"';
# 访问日志 路径 格式(前面定义了)
access_log /var/log/nginx/access.log main;
# 高效文件传输
sendfile on;
#tcp_nopush on;
# 长链接超时间
keepalive_timeout 65;
# 压缩
#gzip on;
# 所有的虚拟主机配置文件
include /etc/nginx/conf.d/*.conf;
}