w i n 10 上使用 n g i n x win10上使用nginx win10上使用nginx
一 下载
中文网:https://nginx.p2hp.com/
官网:http://nginx.org/
http://nginx.org/en/download.html
nginx.exe -s stop //停止nginx
nginx.exe -s reload //重新加载nginx
nginx.exe -s quit //退出nginx
二 常用指令
一、检查配置文件
在重启Nginx之前首先要检查配置文件是否有误:
nginx -t -c ./conf/nginx.conf
如果没有语法错误,则会输出以下信息:
nginx: the configuration file /path/to/nginx.conf syntax is ok
nginx: configuration file /path/to/nginx.conf test is successful
二、修改配置文件
打开nginx.conf文件,并进行修改
三、停止Nginx
在重启Nginx之前需要先停止当前正在运行的Nginx进程。
nginx -s stop
如果当前进程没有停止,则可以使用以下命令来强制停止:
nginx -s quit
四、启动Nginx
start nginx
如果想要启动Nginx并指定配置文件,则可以使用以下命令:
nginx -c ./conf/nginx.conf
五、重启Nginx
重启Nginx时,配置文件会被重新加载。
nginx -s reload
六、设定自启动
如果想要在系统启动时Nginx自动启动,可以将Nginx添加到系统服务中。可以使用以下命令将Nginx添加到服务:
以管理员身份运行cmd,然后运行下面指令
sc create nginx binPath= "D:\nginx-1.24.0\nginx-1.24.0\nginx.exe" displayName= "Nginx" start= "auto"
在上述命令中,binPath为Nginx可执行文件的路径,displayName为服务的显示名称,start为服务的启动类型。
注意:
1.在Windows下重启Nginx需要先检查配置文件,修改需要修改的配置项,然后停止当前正在运行的进程,并重新启动或重启Nginx。
2.可以将Nginx添加到系统服务中,实现自动启动。
反向代理负载均衡
# 方向代理 负载均衡(修改1)
upstream major{
server 127.0.0.1:5000 weight=1;
server 127.0.0.1:6000 weight=1;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
# 反向代理(修改2)
proxy_pass http://major;
client_max_body_size 1024m;
}
实例
#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;
upstream major{
server 127.0.0.1:5000 weight=1;
server 127.0.0.1:6000 weight=1;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://major;
client_max_body_size 1024m;
}
#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;
# }
#}
}