方法一、配置 yum 源 并安装
vi /etc/yum.repos.d/nginx.repo
# nginx yum源
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
# 安装
yum install nginx -y
方法二,安装指定版本
rpm -ivh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.16.1-1.el7.ngx.x86_64.rpm
nginx 总配置文件 /etc/nginx/nginx.conf
没有被
{}
包裹的部分为全局配置,如worker_processes 1;
设置工作进程(子进程)数为 1
events {}
为 nginx 连接配置的模块,如worker_connections 1024;
设置每一个子进程最大允许连接 1024 个连接
http {}
为 nginx http 核心配置模块
server {}
为虚拟主机配置模块,包括监听端口、监听域名等
location {}
URI 匹配
# 运行用户,默认是nginx,可以不进行设置
user nginx;
#Nginx进程,一般设置和cpu核数一样
worker_processes 1;
# 错误日志存放位置
error_log /var/log/nginx/error.log warn;
# 进程pid存放位置
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; # nginx访问日志的存放位置
sendfile off; # 是否开启高效传输模式 on开启 off关闭
#tcp_nopush on; # 减少网络报文段的数量
keepalive_timeout 65; # 保持连接的时间,也叫超时时间
#gzip on; # 开启gzip压缩模式
include /etc/nginx/conf.d/*.conf; # 包含的子配置项的位置和文件
}
nginx 默认子配置文件 /etc/nginx/conf.d/default.conf
server {
listen 80; #配置监听端口
server_name localhost; #配置域名
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html; #服务默认启动目录
index index.html index.htm; #默认访问文件
}
#error_page 404 /404.html; # 配置404页面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #错误状态码的显示页面,配置后需要重启
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#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
#
#location ~ /\.ht {
# deny all;
#}
}