nginx学习四:基础配置
访问nginx首页
-
确保nginx已安装
-
启动nginx
systemctl start nginx.service
-
查看nginx
# 查看nginx pidcat /var/run/nginx.pid# 查看nginx进程ps -aux | grep nginx# 查看网络状态netstat -nltp
-
发现浏览器访问 192.168.190.58:80 访问不到nginx首页问题
# 查看nginx状态systemctl status nginx# nginx检查nginx -t# 查看nginx服务是否启动ps aux|grep nginx# 查看 80 端口是否分配给了nginxnetstat -nltp# 80端口可能没开放,对80端口进行防火墙配置firewall-cmd --zone=public --add-port=80/tcp --permanent–zone #作用域–add-port=80/tcp #添加端口,格式为:端口/通讯协议–permanent #永久生效,没有此参数重启后失效# 重启防火墙systemctl restart firewalld.service# 再次查看nginx页面,发现成功# 查看已经开放的端口firewall-cmd --list-ports
nginx配置详解
-
CoreModule 核心模块,全局配置
CoreModule层下可以有Event、Http
-
EventModule 事件驱动模块
-
HttpCoreModule http内核模块
HTTP模块层允许有多个Server层,Server主要用于配置多个网站。
Server层又允许有多个Location,Location主要用于定义网站访问路径
# 公共的配置定义在 http{}http { // http层开始 //使用 server 配置网站,每个 server{} 代表一个网站(简称虚拟主机) server { listen 80; //监听端口,默认80 server_name localhost; //提供服务的域名或主机名 access_log host.access.log //访问日志 //控制网站访问路径 location / { root /usr/share/nginx/html; //存放网站代码路径 index index.php index.html index.htm;//服务器返回的默认页面文件 } //指定错误代码,统一定义错误页面,错误代码重定向到新的location error_page 500 502 503 504 /50x.html }}
/etc/nginx/nginx.confg详解
# CoreModule核心模块user nginx;#nginx进程所使用的的用户worker_processes 1; # nginx运行的work进程数量(建议与CPU数量一致,或 auto)error_log /var/log/nginx/error.log wa; # 错误日志存放处pid/var/run/nginx.pid; # nginx服务运行后产生的pid进程号# events事件模块events {worker_connections 1024; # 每个worker进程支持的最大连接数use epoll;# 事件驱动模型,默认epoll}# http模块http {include/etc/nginx/mime.types;default_type application/octet-stream;cclog_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;sendfileon;#tcp_nopush on;keepalive_timeout 65;#gzip on;include /etc/nginx/conf.d/*.conf;//重要}
过滤掉注释和空行
[root@localhost conf.d]# egrep -v '^$|^.*#' default.confserver {listen80;server_name localhost;location / {root/usr/share/nginx/html;index index.html index.htm;}error_page500 502 503 504 /50x.html;location = /50x.html {root/usr/share/nginx/html;}}
Nginx配置网站
目的:当我们访问 game.oldboy.com的时候访问 /oldboy_code/ 里面的页面代码
1、在 /etc/nginx/conf.d/ 下新建配置文件,要求一定是 .conf 结尾,然后将默认配置文件关闭,注释
cd /etc/nginx/conf.dmv default.conf default.offvim game.conf# game.confserver { listen 80; server_name game.oldboy.com; location / { root /oldboy_code; index index.html; }}# nginx -t 做语法测试[root@localhost conf.d]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful
2、获取网页代码
# mkdir -p /oldboy_code/(www,bbs,blog)mkdir -p /oldboy_code/vim index.html
3、启动或重载nginx
systemctl start nginxsystemctl reload nginx (建议使用)nginx -s reload
4、nginx访问出现 403 的问题
# 查看 log 日志cat /etc/log/nginx/error.log2020/03/30 01:08:24 [error] 5357#5357: *22 "/oldboy_code/index.html" is forbidden (13: Permission denied), client: 192.168.190.156, server: game.oldboy.com, request: "GET / HTTP/1.1", host: "192.168.190.58"原因:1、由于启动用户和nginx工作用户不一致,将nginx.conf的user改为和启动用户一致vim /etc/nginx/nginx.conf user=rootnginx -tsystemctl reload nginx2、缺少 index.html 等指定的文件3、权限问题修改web目录的读写权限,或者把nginx的启动用户改成目录的所属用户,重启nginxchmod -R 755 /var/www4、SELinux设置为开启状态(enabled)的原因首先查看本机SELinux的开启状态,如果SELinux status 参数为enabled即为开启状态sestatus -v临时关闭(不用重启)setenforce 0永久关闭(需要重启)修改配置文件 /etc/selinux/configvi /etc/selinux/config#SELINUX=enforcingSELINUX=disabledreboot生效
5、通过域名访问,要修改本地 hosts 文件
cat /etc/hosts192.168.190.58 game.oldboy.com
排错
-
域名解析问题
-
nginx无法启动
-
是否端口被占用
lsof -i:80
-
配置文件写错了
-
-
重载失败
-
配置文件写错了
就算配置文件写错了,重载也不一定失败,一定要 nginx -t 检测。
-
-
文件路径写错
-
nginx -t 不报错,systemctl reload nginx 不报错
-
访问页面 404
-
一定要查看日志信息
tail /var/log/nginx/error.log
-
-
root访问页面 index.html 改成 index.htm
-
出现 403 forbidden 问题
mv index.html index.htm
-
作者:KbMan
来源链接:https://www.cnblogs.com/KbMan/p/14339435.html
版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。
2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。