起因
最近在公司接个任务,说用nginx做静态资源服务器,给客户端写个更新程序,把更新包放nginx服务器上。当然nginx作为一款轻量级的web服务器/反向代理服务器/邮件服务器,对于这简单的任务毛毛雨啦
,在此总结一下nginx的使用,以备遗忘时翻看。
Nginx使用
1.下载nginx 直接官网下载:nginx.org
2.下载完成后是个压缩包,这里我把它解压到E:\nginx(以下记录均以此目录为基准)
3.nginx配置文件为根目录下conf\nginx.conf(对应nginx的使用关键在于此配置文件)
4.启动nginx cmd进入nginx根目录: E:\nginx start nginx 即启动nginx
Nginx常用控制台命令
start nginx 启动nginx
tasklist /fi "imagename eq nginx.exe" 查看nginx进程
nginx -s quit 安全关闭
nginx -s stop 强制关闭
nginx -s reload 改变配置文件时,重启nginx工作进程,使配置生效
nginx -s reopen 打开日志文件
nginx -v 查看版本
nginx -h 查看帮助信息
特别提醒!!!
在说Nginx配置文件之前,特别提醒一下,我踩到的一个坑。
Nginx在Windows系统下 查看/修改 conf配置文件
千万不要用记事本打开!
千万不要用记事本打开!
千万不要用记事本打开!碰都别碰!
因为在Windows系统下,记事本打开会用 utf-8-Bom 格式打开文件,即使你的文件是 utf-8 的,他也会自以为是的给你加上Bom头,以他的格式重新编码存储,这种格式将破环nginx配置文件,虽然看上去内容没有任何改变,但重要的编码却被他偷梁换柱了,将导致nginx启动异常。
这是我有一天手残用记事本打开配置后,nginx便无法正常启动了。查看配置,内容都没改变,前两天还好好的呀。于是查看logs/error.log 文件查看错误信息:
2018/07/05 11:17:59 [emerg] 12268#4676: unknown directive "" in E:\nginx-1.15.0/conf/nginx.conf:5
几经波折,终查到原因,windows记事本修改了文件的编码格式,于是用Notepad++打开,修改编码使用UTF-8编码,文件另存,故障排除。
有关UTF-8-BOM格式与UTF-8格式更详细的区别,推荐参看:
Nginx配置文件
Nginx主要由conf文件夹下nginx.conf文件配置,以下为我本地的配置文件,红字部分需特别注意,作为静态资源服务器的配置部分由黄底红字标示。
#user nobody;
# 指定nginx进程数
worker_processes 1;
# 全局错误日志及PID文件
#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服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型,类型由mime.type文件定义
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 指令指定 nginx是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用
sendfile on;
#tcp_nopush on;
# 连接超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
#开启gzip压缩,压缩html
#gzip on;
###################################
# 设定负载均衡的服务器列表 支持多组的负载均衡,可以配置多个upstream 来服务于不同的Server.
# nginx 的 upstream 支持几种方式的分配
# 1.轮询(默认)每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
# 2.weight 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
# 3.ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题
# 4.fair
# 5.url_hash #Urlhash
upstream mysvr{
#weigth参数表示权值,权值越高被分配到的几率越大
#1.down 表示当前的server暂时不参与负载
#2.weight 默认为1 weight越大,负载的权重就越大
#3.backup 其他所有的非backup机器down或者忙的时候,请求backup机器,所以这台机器的压力最轻,备用机器
#server 192.168.1.116 down;
#server 192.168.1.116 backup;
#server 192.168.1.142 weight=1;
server 192.168.1.142 weight=1;
}
#####################################
# 配置代理服务器的地址,即Nginx安装的服务器地址、监听端口、默认地址
server {
#1.监听8099端口
listen 8099;
#对于server_name,如果需要将多个域名的请求进行反向代理,可以配置多个server_name来满足要求
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# 默认主页目录在nginx安装目录的html子目录
root html ;
index index.html index.htm;
#proxy_pass http://mysvr; #跟负载均衡服务器的upstream对应
}
#访问本地E:/source文件夹 访问路径为localhost:8099/file/a.png 实际访问路径为 E:/source/file/a.png
location /file/ {
root E:/source/;
autoindex on;
}
#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;
# }
#}
}
一个简单的负载均衡示例
# 把www.domain.com均衡到本地不同的端口,也可以改为均衡到不同的地址上。
http{
upstream myproject{
server 127.0.0.1:8000 weight=3;
server 127.0.0.1:8001 weight=1;
server 127.0.0.1:8002;
server 127.0.0.1:8003;
}
server{
listen 80;
server_name www.domain.com;
location / {
proxy_pass http://myproject;
}
}
}
以上为我使用nginx做静态资源服务器的nginx配置,下一篇为此对应的C#更新程序的demo程序。
欢迎大家批评指正

本文总结了在Windows下使用nginx作为静态资源服务器的步骤,包括下载安装、配置文件详解、启动方法以及避免记事本修改配置文件导致的问题。特别强调了不要使用记事本编辑nginx配置文件,因为可能会引入BOM头导致启动失败。同时,提供了简单的配置示例。
1741

被折叠的 条评论
为什么被折叠?



