Nginx是一款轻量级Web服务器、反向代理服务器以及电子邮件代理服务器,并且具有高并发连接处理能力和低内存消耗的特点。它也可以用于负载均衡和缓存控制等功能。
功能:
1、yum 安装 2、rpm 3、源码编译安装 4、docker部署安装
环境: Centos8
本次采用yum进行安装,适合新手学习。
# 确定服务器可以访问外网
# 更新yum源
yum update -y
# 安装
yum install nginx
# 使用systemctl管理nginx服务
systemctl start nginx
# 设置开机启动
systemctl enable nginx
# 查看nginx服务状态
systemctl status nginx
# 如果防火墙开启的话需要放通端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
nginx配置文件路径:
# 配置文件一般情况下位于/etc/nginx/nginx.conf,整个目录包含主配置文件nginx.conf和其他配置文件
root@761e1b942a26:/etc/nginx# ls -la
total 24
drwxr-xr-x 1 root root 20 Dec 29 2021 .
drwxr-xr-x 1 root root 19 Nov 13 04:25 ..
drwxr-xr-x 1 root root 26 Nov 13 04:25 conf.d # 配置目录,一般在主配置文件包含
-rw-r--r-- 1 root root 1007 Dec 28 2021 fastcgi_params
-rw-r--r-- 1 root root 5349 Dec 28 2021 mime.types # 网页文本类型配置,一般不需要修改
lrwxrwxrwx 1 root root 22 Dec 28 2021 modules -> /usr/lib/nginx/modules # nginx模块
-rw-r--r-- 1 root root 648 Dec 28 2021 nginx.conf # 主配置文件
-rw-r--r-- 1 root root 636 Dec 28 2021 scgi_params
-rw-r--r-- 1 root root 664 Dec 28 2021 uwsgi_params
# 网页服务器主目录一般在/usr/share/nginx,包括html目录以及对应index.html文件等
root@761e1b942a26:/usr/share/nginx/html# ls
50x.html index.html
# 默认的nginx日志目录/var/log/nginx,包括访问日志和错误日志,根据主配置文件中定义
root@761e1b942a26:/var/log/nginx# ls
access.log error.log
一般情况下的配置: 通过主配置文件定义web服务器的端口和参数,然后将网站资源文件放到服务器网页目录上即可。
nginx配置文件示例: 配置文件才能模块方式编写,使用{}进行同一区块分别,使用#进行注释。
# 定义启动nginx服务器的用户以及进程数量
user nginx;
worker_processes auto;
# 定义错误日志和pid路径
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
# 工作进程连接数
events {
worker_connections 1024;
}
# http块定义了包含server块的整体定义,如显示版本号,是否开启压缩,日志格式等
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 定义了引用指定目录下的配置文件,一般将server块单独定义,一个网站一个server配置文件。
include /etc/nginx/conf.d/*.conf;
}
# server块,可以在conf.d目录下新建一个xx.conf的文件,写入内容。
server {
listen 80; # 表示侦听80端口,有几种写法,如果多个ip地址可以指定地址,否则全部侦听
listen [::]:80; # 侦听ipv6
server_name localhost; # 域名,内网可以直接写ip地址,外网有dns可以写认证域名
# 单独定义这个server的日志
#access_log /var/log/nginx/host.access.log main;
# location定义了网页根目录,以及网页默认文件类型index.html
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# 定义特定404页面返回文件
#error_page 404 /404.html;
# 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;
}
# 配置PHP代理,实现动态网站搭建
# 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;
#}
}
本文由 mdnice 多平台发布