1.nginx命令
1.启动
D:\tool\nginx-1.8.1>start nginx
2.停止
D:\tool\nginx-1.8.1>nginx.exe -s stop
D:\tool\nginx-1.8.1>nginx.exe -s quit
注:stop是快速停止nginx,可能并不保存相关信息;quit是完整有序的停止nginx,并保存相关信息。
3.重新载入Nginx:
D:\tool\nginx-1.8.1>nginx.exe -s reload
4.查看版本:
nginx -v
5.杀死所有nginx进程:
taskkill /f /t /im nginx.exe
2.nginx配置
静态资源配置只能放在 location / 中
【nginx.conf】
#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 ser1 {
server 192.168.1.60:8081;
}
upstream ser2 {
server 192.168.1.60:8081 weight=2;
server 192.168.1.60:8083 weight=3;
}
upstream ser3 {
server 192.168.1.60:8083;
}
upstream ser4 {
server 192.168.1.60:8084;
}
server {
listen 9091;
server_name 192.168.1.60;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root e:/App/;
index index.html index.htm;
}
location ^~/ser1/ {
proxy_pass http://ser2;
root html;
index index.html index.htm;
}
location ^~/ser2/ {
proxy_pass http://ser2;
root html;
index index.html index.htm;
}
location /ser4/ {
proxy_pass http://ser4;
root html;
index index.html index.htm;
}
location ^~/ser3/ {
proxy_pass http://ser3;
root html;
index index.html index.htm;
}
location /download {
# alias D:/tool/nginx-1.8.1/temp/docs;
root /docs;
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;
# }
#}
include conf.d/*.conf;
}
【9092.conf】
server {
listen 9092;
server_name 192.168.1.60;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root e:/App/;
index index1.html index.htm;
}
#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;
# }
#}
3.root、alias、index、proxy_pass
还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的,而root则可有可无~~
3.1 root、index
以下的访问路径相当于:http://192.168.1.60:9091/cfk/ === http://192.168.1.60:9091/nginx/index/cfk,然后index是指先去找index55.html,若无该文件,再接着找index4.html、index5.html,如果没找到则会报错:
但是访问 :http://192.168.1.60:9091/cfk/index3.html 则正确,因为 nginx/index/cfk下是存在index3.html文件的。
若配置正确,则访问成功:【这就是index的作用】
3.2 alias
总结:root是会拼接去找地址;alias是直接对应映射的地址。
参考来源:Windows下Nginx的启动、停止、重启等命令 - 建铭博客 - 博客园
3.3 proxy_pass
在nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的url加/,表示绝对根路径;如果没有/,表示相对路径,把匹配的路径部分也给代理走。
访问http://192.168.1.1/proxy/test.html 时候:
第一种: 相当于:http://127.0.0.1/test.html
location /proxy/ {
proxy_pass http://127.0.0.1/;
}
第二种 : 相当于:http://127.0.0.1/proxy/test.html
location /proxy/ {undefined
proxy_pass http://127.0.0.1;
}
第三种:相当于:http://127.0.0.1/aaa/test.html
location /proxy/ {
proxy_pass http://127.0.0.1/aaa/;
}
第四种 :相当于:http://127.0.0.1/aaatest.html
location /proxy/ {
proxy_pass http://127.0.0.1/aaa;
}