nginx常用命令
- 开启服务:start nginx
- 停止服务:nginx -s stop;nginx -s quit
注:nginx停止命令stop与quit参数的区别在于stop是快速停止nginx,可能并不保存相关信息,quit是完整有序的停止nginx ,并保存相关信息。 - 重启服务:nginx -s reload;也就是nginx热部署,不需要关闭和启动服务;
ngnix配置文件
#user nobody;
# 工作进程数量,一般设置为本机的cpu数量
worker_processes 8;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
# include用来对外部文件配置的引用
include mime.types;#引入外部mine.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 logs/access.log main;
sendfile on; #启动高效传输文件的模式
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;#指定 KeepAlive 的超时时间(timeout),指定每个 TCP 连接最多可以保持多长时间
#gzip on;
server {
listen 8686;#服务监听端口
server_name localhost;#服务名称
#charset koi8-r;
#路由/重定向的文件地址
location / {
root html; #文件夹地址
index index.html index.htm;#默认文件名称
}
#配置一个项目发布
location /project {
alias data/dist; #路由转换到/project时,服务重定向访问文件的位置
index index.html index.htm;
}
location /data3d {
alias data/geobody_3dtiles;
index tileset.json;
add_header Access-Control-Allow-Origin *;#跨域设置
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range,Authorization,Accept,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
location ~ \.php$ {
proxy_pass http://127.0.0.1;#反向代理
}
}
}
root和alias区别
相同点:root 和 alias 都可以定义在 location 模块中,都是用来指定请求资源的真实路径。
不同点:
root:root 则是最上层目录的定义,
如:location /project {
root: dist
}
那么在服务器里面对应的真正的资源路径为:dist/project,即资源路径为root+location指定值的组合
alias: 是一个目录别名的定义
同如:location /project {
alias: dist
}
那么在服务器里面对应的真正的资源路径为:dist,即资源路径为alias制定的路径地址,与location指定值无关。