本站教程收集整理的这篇文章主要介绍了Centos7 yum 源安装nginx以及nginx配置介绍,本站教程本站觉得挺不错的,现在分享给大家,也给大家做个参考。
Centos7 yum 源安装Nginx以及Nginx配置介绍
(1)安装
1.1添加Nginx存储库
@H_801_11@yum install epel-release
1.2开始安装Nginx
@H_801_11@yum install Nginx
中途这两个命令会提示你是否继续 你只需要一路 yyyyyyyyyyy
到这里 Nginx已经安装好了
1.3Nginx启动需要注意的地方
1.Nginx端口是否被占用 ,Nginx默认端口为80端口
2.服务器是否开启80端口防护墙以及开放对应安全组
下方命令为开启80端口防火墙,云服务器的话设置安全组需要登录阿里云或者腾讯云开放安全组
@H_801_vps云服务器11@firewall-cmd --zone=public --add-port=80/tcp --peRMANent
启动试一试!!
附带Nginx常用命令
@H_801_11@#启动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已成功安装并启动了
@H_801_11@Welcome to CentOS
The Community ENTerprise OperaTing System
(2) Nginx配置
使用yum源安装的Nginx配置默认位置在
@H_801_11@/etc/Nginx/Nginx.conf
@H_801_11@# For more @R_211_4036@ion 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 @R_211_4036@ion.
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:1R_370_11845@;
# 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的区别
@H_801_11@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的学习到这就结束了,,后续深入的时候继续更新
-
点赞
-
收藏
-
分享
-
- 文章举报
你是胖虎?
发布了29 篇原创文章 · 获赞 22 · 访问量 3465
私信
关注
本站总结
以上是本站教程为你收集整理的Centos7 yum 源安装nginx以及nginx配置介绍全部内容,希望文章能够帮你解决Centos7 yum 源安装nginx以及nginx配置介绍所遇到的程序开发问题。
如果觉得本站教程网站内容还不错,欢迎将本站教程推荐给好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。