1. 安装
安装命令为:
yum install -y nginx
检查是否安装成功
nginx -v
2. 查询nginx安装目录
rpm -ql nginx
3. nginx.conf基本文件解读
#运行用户,默认即是nginx,可以不进行设置
user nginx;
#Nginx进程,一般设置为和CPU核数一样
worker_processes 1;
#错误日志存放目录
error_log /var/log/nginx/error.log warn;
#进程pid存放位置
pid /var/run/nginx.pid;
events {
worker_connections 1024; # 单个后台进程的最大并发数
}
http {
include /etc/nginx/mime.types; #文件扩展名与类型映射表
default_type application/octet-stream; #默认文件类型
#设置日志模式
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; #nginx访问日志存放位置
sendfile on; #开启高效传输模式
#tcp_nopush on; #减少网络报文段的数量
keepalive_timeout 65; #保持连接的时间,也叫超时时间
#gzip on; #开启gzip压缩
include /etc/nginx/conf.d/*.conf; #包含的子配置项位置和文件
server {
listen 80; #配置监听端口
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; #默认访问文件
}
#error_page 404 /404.html; # 配置404页面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #错误状态码的显示页面,配置后需要重启
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
4. nginx服务启动,停止,重启
4.1 启动
- 在centOS7.4版本里,可直接使用nginx
nginx
- 使用linux命令systemctl(前提是需要配置)
systemctl start nginx.service
4.2 查询服务运行情况
ps aux | grep nginx 或 ps -ef | grep nginx
4.3 停止
- 立即停止
nginx -s stop
- 从容停止
nginx -s quit
- linux命令
systemctl stop nginx.service
- kill (由4.2查询出进程号以后)
kill + 进程id
4.4 重启
nginx -s reload
5. 权限控制
5.1 ip权限
注意配置权限和书写位置有关,下面第一种会禁止所有ip, 下面第二种则会只允许第一个ip访问,禁止剩余所有IP访问
location / {
deny all;
allow 11.11.11.11;
}
location / {
allow 11.11.11.11
deny all
}
5.2 文件夹权限
location = /img {
allow all
}
location = /admin {
deny all
}
5.3 正则匹配
location ~*\.php$ {
deny all
}
6.配置跨域
add_header Access-Control-Allow-Origin $http_origin;
add_header Access-Control-Allow-Credentials true;
7. vue中history配置
location / {
try_files $uri $uri/ /index.html
}
8. 代理
location / {
proxy_pass https://www.baidu.com
}