Centos7 yum 源安装nginx以及nginx配置介绍
(1)安装
1.1添加Nginx存储库
yum install epel-release
1.2开始安装nginx
yum install nginx
中途这两个命令会提示你是否继续 你只需要一路 yyyyyyyyyyy
到这里 nginx已经安装好了
1.3nginx启动需要注意的地方
1.nginx端口是否被占用 ,nginx默认端口为80端口
2.服务器是否开启80端口防护墙以及开放对应安全组
下方命令为开启80端口防火墙,云服务器的话设置安全组需要登录阿里云或者腾讯云开放安全组
firewall-cmd --zone=public --add-port=80/tcp --permanent
启动试一试!!
附带nginx常用命令
#启动nginx 两种命令都可以启动
systemctl start nginx
nginx
#设置nginx开机启动
systemctl enable nginx
#卸载nginx命令
yum remove nginx
#停止nginx
nginx -s stop
#重启nginx
nginx -s reload
如果没有修改nginx默认端口 nginx启动后直接访问服务器Ip(80可默认不写) 即可,如果修改了默认端口,访问方式为服务器 ip:nginx端口
访问后网页出现一下提示则说明nginx已成功安装并启动了
Welcome to CentOS
The Community ENTerprise Operating System
(2) nginx配置
使用yum源安装的nginx配置默认位置在
/etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
#运行用户
user nginx;
#启动进程,每个Nginx进程平均耗费10M~12M内存。建议指定和CPU的数量一致即可
worker_processes auto;
#全局错误日志及PID文件
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
#工作模式
events {
#单个后台worker process进程的最大并发链接数
worker_connections 1024;
}
#http服务器,负载均衡以及反向代理都是使用它
http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
#设定mime类型,类型由mime.type文件定义
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
#server 块是对虚拟主机的配置,server标志定义虚拟主机开始
server {
#listen用于指定虚拟主机的服务端口,启动后就是虚拟机ip+此指定的端口进行访问
listen 80 default_server;
listen [::]:80 default_server;
#server_name 用来指定IP地址或域名,多个域名之间用空格分开
server_name _;
#root指令用于指定虚拟主机的网页根目录,这个目录可以是相对路径,也可以是绝对路径
root /usr/share/nginx/html;
#加载默认服务器组的配置文件.
include /etc/nginx/default.d/*.conf;
#location块
#URL地址匹配 /a 访问就ip:端口/a
#下方location是我自己目前的配置
#在一个server中 可以配置多个location ,但是“/“必须有一个
location / {
#ftp文件主目录,只允许有一个
root /var/ftp/pub/;
#此访问的首页
index index.html index.htm index.php index.jpg index.mp4 index.txt;
}
#404页
error_page 404 /404.html;
location = /40x.html {
}
#500页
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
#第二个location
location /chexuan/ {
alias /ftp/chexuan/;
index index.html index.htm index.php index.jpg index.mp4 index.txt;
}
#举例
#访根目录/, 比如http://localhost/ 就会匹配 /var/ftp/pub/下的index文件
#访问目录/chexuan/ 比如http://localhost/chexuan/ 就会匹配 /ftp/chexuan/下的index文件
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
其中location代码块中 root 和alias的区别
root实例:
location /t/ {
root /www/root/html/;
}
如果一个请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/t/a.html的文件。
alias实例:
location /t/ {
alias /www/root/html/leilei/;
}
如果一个请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/leilei/a.html的文件。注意这里是leilei,因为alias会把location后面配置的路径丢弃掉,把当前匹配到的目录指向到指定的目录。
注意:
- 使用alias时,目录名后面一定要加"/"。
- alias在使用正则匹配时,必须捕捉要匹配的内容并在指定的内容处使用。
- alias只能位于location块中。(root可以不放在location中)
目前Nginx的学习到这就结束了,,后续深入的时候继续更新