# nginx相关

# 常用命令

# windows环境

nginx的安装目录下,即nginx.exe所在的目录执行:

# 启动
start nginx    //启动

# 停止
nginx -s stop    // 停止nginx

# 重启
nginx -s reload   // 重新加载配置文件并重起

# linux环境

# 启动命令
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

# 停止
/usr/local/nginx/sbin/nginx -s  stop

# 重启
/usr/local/nginx/sbin/nginx -s  reload

# 查看进程命令
ps -ef | grep nginx

# 关闭进程
kill -HUP Nginx主进程号

TIP

-c:制定配置文件的路径

不加-nginx会自动加载默认路径的配置文件

# root与alias

# alias

location /img/ {
  alias /var/www/image/;
}

若按照上述配置的话,则访问/img/目录里面的文件时,ningx会自动去/var/www/image/目录找文件

# root

location /img/ {
  root /var/www/image;
}

若按照这种配置的话,则访问/img/目录下的文件时,nginx会去/var/www/image/img/目录下找文件。

TIP

alias是一个目录别名的定义,root则是最上层目录的定义。

还有一个重要的区别是alias后面必须要用/结束,否则会找不到文件的。而root则可有可无

# 常用例子

# 主配置

#user XX administrator administrators;
worker_processes  2;

#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 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,
    #对于普通应用,必须设为 on,
    #如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,
    #以平衡磁盘与网络I/O处理速度,降低系统的uptime.
    sendfile        on;
    #tcp_nopush     on;

	#连接超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;

	#开启gzip压缩
	gzip on;

	#不压缩临界值,大于1K的才压缩,一般不用改
	gzip_min_length 10k;
	gzip_buffers 4 16k;

	#用了反向代理的话,末端通信是HTTP/1.0;默认是HTTP/1.1
	#gzip_http_version 1.0;

	#压缩级别,1-10,数字越大压缩的越好,时间也越长
	gzip_comp_level 2;

	# nginx 做前端代理时启用该选项,表示无论后端服务器的headers头返回什么信息,都无条件启用压缩
    gzip_proxied any;

	#进行压缩的文件类型,缺啥补啥就行了,JavaScript有两种写法,最好都写上吧,总有人抱怨js文件没有压缩,其实多写一种格式就行了
	#如果开启了对js压缩,则在Response Headers里可以看到,Content-Encoding:gzip,Transfer-Encoding:chunked
	gzip_types text/plain application/x-javascript text/css application/xml application/javascript text/javascript application/x-httpd-php image/jpeg image/gif image/png;

	#跟Squid等缓存服务有关,on的话会在Header里增加"Vary: Accept-Encoding",我不需要这玩意,自己对照情况看着办吧
	gzip_vary off;

	#IE6对Gzip不怎么友好,不给它Gzip了
	gzip_disable "MSIE [1-6]\.";

	#设定请求缓冲
    #client_header_buffer_size    128k;
    #large_client_header_buffers  4 128k;

	#包含其它配置文件
	include nginx-fl.conf;
}

# 子配置

#设定日志格式
log_format  fl  '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"';

upstream door_server{
	server localhost:8280;
}

server {
	listen       8088;
	server_name  localhost;

	client_max_body_size 700M;
  charset utf-8;

	location / {#请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
		root	html;
		add_header 'Access-Control-Allow-Origin' *;
	  #	index  index.html index.htm;
		# deny 127.0.0.1;  #拒绝的ip
		# allow 172.18.5.54; #允许的ip
	}

	location /door-api/ {
			#access_log off;
			proxy_connect_timeout 300;
      proxy_read_timeout 300;
      proxy_send_timeout 300;
			proxy_pass http://door_server/;
	}

	location ^~ /rsm-api {
		add_header 'Access-Control-Allow-Origin' *;
		alias C:/resource/;
	}

	location /test/ {
		proxy_cache cache_one;
		proxy_cache_valid 200 302 24h;
		proxy_cache_valid 301 30d;
		proxy_cache_valid any 5m;
		#proxy_cache_valid 200 302 30m;
		#proxy_cache_revalidate on;
		expires 300m;
		add_header X-Cache-Status $upstream_cache_status;
		add_header 'Access-Control-Allow-Origin' *;
		alias C:/test/;
	}

	access_log  logs/fl.access.log fl;

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#禁止访问 .htxxx文件
	location ~ /\.ht {
		deny  all;
	}
}

server {
	listen       8001;
	server_name  localhost;

	client_max_body_size 700M;
  charset utf-8;

	location /test/ {
		add_header 'Access-Control-Allow-Origin' *;
		alias C:/test2/;
	}
}

# 配置websocket

upstream ws_server{
	server localhost:7002;
	keepalive 1000;
}

map $http_upgrade $connection_upgrade {
	default upgrade;
	'' close;
}

server {
	location /ws-api {
		proxy_pass http://ws_server/file;
		proxy_http_version 1.1;
		proxy_redirect off;
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_read_timeout 3600s;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection $connection_upgrade;
   }
}

# 配置history路由

server {
	listen       3001;
	server_name  localhost;

	client_max_body_size 700M;
  charset utf-8;

	location / {
		root	html;
		add_header 'Access-Control-Allow-Origin' *;
		index  index.html index.htm;
		try_files $uri $uri/ /index.html; # 主要是这句,将没找到的地址,最终又转到index.html
	}
}