windows 安装 使用 nginx
nginx官网下载地址:https://nginx.org/en/download.html
下载稳定版本即可
下载压缩包解压到即可
- 进入文件夹中,打开命令行窗口,执行启动命令
start nginx.exe
- 验证(默认是80端口)(出现欢迎页面即成功)
- 常用命令
查看版本:nginx -v
验证nginx配置文件:nginx -t
启动: start nginx.exe
立即停止:nginx -s stop
重新加载:nginx -s reload
完整有序停止:nginx -s quit
配置信息详解
- nginx配置文件
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
- 设置worker进程的用户,指的linux中的用户,会涉及到nginx操作目录或文件的一些权限,默认为nobody
#user nobody;
- worker进程工作数设置,一般来说CPU有几个,就设置几个,或者设置为N-1也行
worker_processes 1;
- nginx 日志级别debug | info | notice | warn | error | crit | alert | emerg,错误级别从左到右越来越大
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
设置nginx进程 pid
#pid logs/nginx.pid;
每个worker允许连接的客户端最大连接数
worker_connections 1024;
keepalive_timeout设置客户端与服务端请求的超时时间,保证客户端多次请求的时候不会重复建立新的连接,节约资源损耗
keepalive_timeout 65;
gzip
启用压缩,html/js/css压缩后传输会更快gzip on;
server模块
listen 80; //监听端口
server_name localhost; //域名或ip
//请求路由映射,匹配拦截
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
多个localhost优先级
- 精确匹配(
=
) > 前缀匹配(^~
)> 正则匹配(~
或~*
)> 常规路径前缀匹配 > 通配符匹配(/
)
root和alias
root
是将 URI 直接追加到指定的目录路径后,而 alias
是替换掉匹配的 location 部分
- root
location /image {
root html/;
index index.html index.htm;
}
访问:http://localhost/image/3.jpg 实际映射到 html/image/3.jpg
- alias
location /image {
alias html/;
index index.html index.htm;
}
访问:http://localhost/image/3.jpg 实际映射到 html/3.jpg
注意: alias 后面必须要用 “/” 结束,否则会找不到文件,而 root 则对 ”/” 可有可无。