第一篇和这篇的时间跨度有点大,间隔了快2个月。因为中间加急搞了一个jenkins自动化构件的东西,然后工厂又提了个蓝牙Ibeacon测距的需求,做了个demo让他们去做实验,现在终于有时间把这个nginx来实际操作一下了。
一、安装
去官网 下载 Nginx,以Stable version为例。
[root@centos]# yum install yum-utils
[root@centos]# vim /etc/yum.repos.d/nginx.repo
//这里只用稳定版,所以只把以下内容拷贝进去即可
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[root@centos]# yum install nginx
nginx -s quit
正常退出,必须在开启服务器的同一个用户下操作,比如root用户开就必须root用户来执行该语句,注意:修改配置后用该语句停止服务再重新启动服务,配置不会生效
nginx -s reload
加载配置项,只要修改配置就要调用该语句
nginx -s stop
快速结束服务
在centos下也可用systemctl来操作
systemctl restart nginx.service
systemctl stop nginx.service
用whereis nginx查找文件目录,找到配置文件nginx.conf
/etc/nginx/nginx.conf
/etc/nginx/conf.d/*.conf
打开selinux权限
[root@centos]# cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M nginx_limit
[root@centos]# semodule -i nginx_limit.pp
修改并开放端口
端口修改在/etc/nginx/conf.d/default.conf
listen 9889;默认80,这里改为9889
[root@centos]# firewall-cmd --zone=public --add-port=9889/tcp --permanent
[root@centos]# firewall-cmd --reload
二、配置静态资源服务
配置静态资源服务主要在/etc/nginx/conf.d/default.conf里
这个地方调试信息要用到以下目录文件,主要是error.log
/var/log/nginx/error.log
/var/log/nginx/access.log
先看配置文件
server {
listen 9889;
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;
}
location /webpage/ {
root /home/server/static;
index index.html;
}
............省略其他内容..........
}
location /
文件目录是: /usr/share/nginx/html
url访问地址:http://localhost:9889
会展示 /usr/share/nginx/html/index.html的文件内容
location /webpage/
文件目录是:/home/server/static
url访问地址:http://localhost:9889/webpage
会展示 /home/server/static/webpage/index.html的文件内容
注意
1、匹配的时候会先匹配下级内容,先/webpage/然后才匹配/
2、注意配置/webpage/时,最后的url地址要带webpage,而root /home/server/static;这个配置项,是webpage目录的上级目录,否则访问的时候会各种404
遇到的问题
1、403 Forbidden
首先可能是配置的目录有问题,如果确认目录都没问题,就要看看文件是否有访问权限,在nginx.conf文件里把user nginx;改为user root;










网友评论