nginx 基础

1.nginx 下载安装 及主要原理

原理:不等待数据加载,所以一个进程可以同时处理上千个请求。nginx启动之后会启动1个master进程处理nginx内部核心逻辑,多个worker 线程,专门用来处理nginx服务

2.nginx 常用命令

(1)nginx -? :查看nginx命令帮助
(2)nginx -t :检查nginx.conf 文件的语法
(3)nginx -T:检查nginx.conf 文件的语法
(4)nginx -v:查看nginx 的版本
(5)nginx -V:查看nginx的版本,可以输出nginx 打开的module
(6)nginx -s [reopen|reload|stop|quit]:给nginx发送消息
reopen:修改日志文件输出位置后,需要reopen,配置才会生效
reload:重新加载nginx.conf 文件
stop:立即停止nginx
quit:优雅的停止nginx

3.nginx.conf 文件样例

#user  nobody;#nginx 服务启动用户
worker_processes  1;#工作线程数量

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  debug; #error日志输出的位置及日志级别

#pid logs/nginx.pid;


events {
   debug_connection 192.168.0.147; 
    worker_connections  1024;
}


http {
    include mime.types;#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"';

	#日志输出位置,注意:后面的main是固定的
    #access_log logs/access.log main;

    sendfile on;
    #tcp_nopush on;

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

    #gzip on;

	#server 必须在http中,一个http中可以有多个server站点
    server {
		#监听端口号
		listen 80;
		#监听的域名,可以有多个域名,多个域名之间使用空格隔开
		server_name www.luban.com;
		#根目录
		root /www/luban/;
		access_log logs/$host.access.log  main;
	   
		location / {
			index  index.html index.htm;
			#指定只有【.luban.com】结尾的网站才可以加载次location
			valid_referers none blocked *.luban.com;
			if ($invalid_referer) {
				return 403;
			}
		}
		
		location /old/ {
			#别名
			alias /www/old_luban/;
			index  index.html index.htm;
		}
 
		location = /old/baidu {
			proxy_pass http://www.baidu.com/;
		}	
	
		location ~ \.(png|jpg|gif|avi|mp4|css|js)$  {
			root /www/static/;
			valid_referers none blocked *.luban.com;
			if ($invalid_referer) {
				return 403;
			}
		}
 
    }
    server {
        listen 80;
        server_name localhost;

        #charset koi8-r;

        #access_log logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
     
		location = /basic_status {
				stub_status;
		}

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

}

4.root 和alias 的区别

root:可以放在server、location中,location中的root优先级更高,root不会把前缀去掉,例如:

location /action/test {
	root /home/static;
}
#请求的路径是/action/test/index.html,最后映射的完整地址是:
/home/static/action/test/index.html

alias:别名,只能配置在location中,会把前缀替换掉,例如:

location /action/test {
	alias /home/static;
}
#请求的路径是/action/test/index.html,最后映射的完整地址是:
/home/static/index.html

5.location 前缀匹配规则

(1)= 精确匹配
(2)/ 前缀匹配
(3)~正则表达式,区分大小写匹配
(4)~* 正则表达式,不区分大小写匹配

6.location 匹配优先级

优先级从高到低:
(1)= 精确匹配
(2)正则匹配
(3)前缀最大匹配原则
(4)配置靠前优先

7.限防盗链配置

加入到指定的location中:

valid_referers none blocked *.xxx.com;
 if ($invalid_referer) {
       return 403;
}

8.动静分离

静态资源加载:
(1)如果静态资源的路径前缀是固定的,推荐使用 前缀匹配

location /static {
	alias /home/static;
}

(2)正则表达式:

location ~* \.(css|png|jpg|js)$ {
	alias /home/static;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值