启动
安装nginx后,进入nginx根目录, cmd环境执行 start nginx.exe
,即可启动.
进入 http://127.0.0.1/
可以看到初始化页面.
其他命令
需要在nginx根目录运行.
nginx.exe -s stop //停止nginx
nginx.exe -s reload //重新加载nginx
nginx.exe -s quit //退出nginx
配置
nginx配置文件为 /conf/nginx.conf
同时运行两个web程序
配置两个server即可. 例子:
server {
listen 80;
server_name localhost;
location / {
root html;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /prod-api{
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080/;
}
}
server {
listen 90;
server_name localhost;
location / {
root admin_html;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /prod-api{
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080/;
}
}
日志切分
nginx自身并不能够切分或滚动日志,因此只能用一个bat脚本按天切割日志,并删除三天前的日志.
将此bat脚本设置定时任务明天0点运行即可:
@echo off
rem nginx滚动日志
rem nginx工作目录
set workspace=D:\luozixi\nginx-1.16.0
rem 日志存放目录
set logdir=D:\luozixi\nginx-1.16.0\logs
rem 将当前日志重命名,用今日的日期
move %logdir%\access.log %logdir%\access_%date:~0,4%_%date:~5,2%_%date:~8,2%.log
move %logdir%\error.log %logdir%\error_%date:~0,4%_%date:~5,2%_%date:~8,2%.log
rem 重新打开日志文件,如果不做这一步,nginx会继续往已被重命名的日志文件中写入日志
%workspace%\nginx.exe -s reopen -p %workspace%
rem 删除三天前的日志
set DaysAgo=3
forfiles /p %logdir% /m *.log /d -%DaysAgo% /c "cmd /c del /f /q @path"
pause