- 安装nginx
执行yum install nginx
- 查看nginx的文件
执行 rpm -ql nginx
- 常用的配置和文件有
nginx/conf.d/default.conf
nginx/nginx.conf (下面的配置都是在此文件)
- nginx启动
执行nginx或执行systemctl start nginx.service
检查是否启动成功执行ps aux | grep nginx;成功一般有3条记录
- nginx停止
立即:nginx -s stop
从容:nginx -s quit
- 6.nginx重启/重载
执行systemctl restart nginx.service
重载 nginx -s reload
- 7.访问控制
location / {
deny 123.9.51.42;
allow 232,123,123,2;
}
=号代表精确匹配
location =/img{
allow all;
}
正则匹配
location ~\.php$ {
deny all;
}
- 配置虚拟主机
虚拟主机就是可以在服务器上配置多个域名;一般通过设置端口号来实现;
server{
listen 8001;
server_name localhost;
root /usr/share/nginx/html/html8001;
index index.html;
}
- 反向代理
server{
listen 80;
server_name test12.com;
location / {
proxy_pass http://test34.com;
}
}
- nginx判断移动和pc
server{
listen 80;
server_name test.com;
location / {
root /usr/share/nginx/pc;
if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
root /usr/share/nginx/mobile;
}
index index.html;
}
}
- nginx 开启gzip压缩
http {
.....
gzip on;
gzip_types text/plain application/javascript text/css;
.....
}
- nginx 配置跨域
location /apis { #添加访问目录为/apis的代理配置
rewrite ^/apis/(.*)$ /$1 break;
proxy_pass http://localhost:82;
}
网友评论