nginx 家常用法 --windows 环境

179 阅读1分钟

[toc]

windows 环境

指定配置文件

nginx.exe -c <config文件路径>

重启

nginx.exe -s reload 重启的时候会自动使用之前的配置文件

nginx.exe -s reopen 也有重启的效果, 具体区别未知

停止

nginx.exe -s stop stop表示立即停止nginx, 不保存相关信息

nginx.exe -s quit quit表示正常退出nginx, 并保存相关信息

kill 关闭进程

windows环境下 快速杀死nginx.exe taskkill /f /t /im nginx.exe

扩展

windows环境下 快速杀死占用端口的进程 小工具 csdn上看到的

保存为 bat 脚本,设置需要解除占用的端口 port,点击运行即可

@echo offsetlocal enabledelayedexpansion
set prot = 8022
for /f "delims=  tokens=1" %%i in ('netstat -aon ^| findstr "%prot %"') do (
set a=%%i
goto js
)
:js
taskkill /f /pid "!a:~71,5!"
pause>nul
查看 / 杀死占用端口的进程

netstat -ano | findstr "8007"   //查看端口号,所在的进程号。

taskkill /f /pid 10012   //杀死进程。

配置项

普通代理

server {
    listen       8081;
    server_name  localhost;
	
    location / {
        proxy_pass   http://localhost:8080/;
    }
    location /api {
        proxy_pass   http://localhost:9002/;
    }
}

websocket代理

本节参考: www.jianshu.com/p/0244dfd13…

因为webSocket协议是基于http协议升级的,所以可以使用nginx反向代理webSocket.

location /sockjs-node {
         proxy_pass http://localhost:8080/;
         proxy_http_version 1.1;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection "Upgrade";
         proxy_set_header X-Real-IP $remote_addr;
 }
代理webSocket的关键参数

proxy_set_header Upgrade
把代理时http请求头的Upgrade 设置为原来http请求的请求头,wss协议的请求头为 websocket

proxy_set_header Connection 因为代理的wss协议,所以http请求头的Connection设置为Upgrade

proxy_set_header X-Real-IP 给代理设置原http请求的ip,填写$remote_addr 即可

完整例子


#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;	

    sendfile        on;
    keepalive_timeout  65;

    #gzip  on;
	map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }
    server {
        listen       8081;
        server_name  localhost;
		
		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection $connection_upgrade;
		proxy_set_header X-real-ip $remote_addr;
		proxy_set_header X-Forwarded-For $remote_addr;
        location / {
            proxy_pass   http://localhost:8080/;
        }
		location /api {
			proxy_pass   http://localhost:9002/;
		}
    }

}

PS: 可能有很多不全的, 且当个笔记记在这吧