一台云服务器Windows篇:06、Nginx

Nginx 的安装与基本配置

Nginx

是一个HTTPWeb服务器、反向代理、内容缓存、负载均衡器、TCP/UDP代理服务器、和邮件代理服务器。

官方初学者指南:Beginner’s Guide

官方文档:nginx documentation

Nginx for Windows

下载:nginx: download

Windows 版本官方说明:nginx for Windows

  1. 是个beta版本,因为使用WIN32 API不应期望高性能和可伸缩性;
  2. 除了其他已知问题,功能上与UNIX版本几乎一样。另外,有的模块是没有Win版本的,比如自动缓存清理。
  3. 可以启动多个worker,但只有一个在工作;官方没有以系统服务方式运行。

Nginx启动/关闭/重新加载配置

命令说明
nginx启动 Nginx,需要在当前目录下被执行 或指定启动目录。
比如:start /d %nginxDir% %nginxDir%\nginx.exe
WIN开机自启动实现方式:
1.在计划任务中,不登录自启动。
2.结合 Autologon 开机自动登陆,在启动文件中添
3.利用第三工具(比如:NSSM – the Non-Sucking Service Manager 、 window service wraper),安装为系统服务。
nginx -s quit平稳关闭 Nginx
nginx -s stop停止 Nginx,或者直接杀进程(taskkill /f /FI “IMAGENAME eq nginx.exe”)
nginx -t检查配置文件的正确性
nginx -s reload平稳重启,在所有连接都结束才会重新加载配置文件启动。
nginx -v查看 Nginx 版本

配置 conf\nginx.conf

生成一个没有注释的配置文件

  • /V 只打印不包含匹配的行。
  • /R 将搜索字符串作为一般表达式使用。
  • 更多参数使用 findstr /? 查看。
d:\nginx>ren .\conf\nginx.conf nginx.conf.initial
d:\nginx>findstr /v /r # .\conf\nginx.conf.initial > .\conf\nginx.conf

在这个配置下,就可以通过域名访问nginx解压出来子目录html内容了。

配置文件中,所有涉及磁盘目录的路径,是以【/】代替【\】,比如C:\nginx\html 要写成 C:/nginx/html

我的配置文件

将日志文件,按月份+域名自动拆分。

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    tcp_nopush      on;
    keepalive_timeout  65;
    gzip on;
    gzip_types text/css text/xml image/gif image/jpeg application/javascript text/plain image/png image/x-icon application/json;
    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;
    
# --- 默认的80端口 ---
    server {
        listen       80 default_server;
        server_name  "";
        
        if ($time_iso8601 ~ "^(\d{4})-(\d{2})") {
            set $year $1;
            set $month $2;
        }
        access_log  logs/access-$year$month-$host.log main;
                
        location / {
            root   html;
            index  50x.html;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

# --- y.z start ---
    server {
        listen       80;
        server_name  y.z;
        
        if ($time_iso8601 ~ "^(\d{4})-(\d{2})") {
            set $year $1;
            set $month $2;
        }
        access_log  logs/access-$year$month-$host.log main;
        
        location / {
            root   C:/webData/wwwroot;
            index  index.php index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
# --- end ---
}

开机自启动nginx

下载:Autologon – Sysinternals | Microsoft Learn

  1. 配置开机自动登录
  2. 配置自启动bat

@echo off
REM 与 nginx.exe 同目录
taskkill /f /FI "IMAGENAME eq nginx.exe"
start /d %~dp0 %~dp0nginx.exe
exit

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注