nginx配置

一:Nginx介绍

二:Nginx配置

Nginx可以充当虚拟主机

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;



        location / {
            root   G:\program\web-demo\html;
            index  index.html index.htm;
        }

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
                root G:\program\web-demo;
                if (-f $request_filename) {
                expires 1d;
                break;
                }
        }
    
        location ~ .*\.(js|css)$
                {
                root G:\program\web-demo;
                if (-f $request_filename) {
                expires 1d;
                break;
        }
	}

	location ~* \.(eot|ttf|ttc|otf|eot|woff|woff2|svg)$ {
                root G:\program\web-demo;
                add_header Access-Control-Allow-Origin *;
		}

	
location ~ (.*\.json)

    {

        root  G:\program\web-demo;

      error_page 405 =200 $1;

    }

	
	

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

  Nginx-反向代理配置

 

 Nginx-负载均衡配置

Nginx 虚拟主机配置 通过域名

   server {
        listen       80;
        server_name 101.200.242.66;
       

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
          root  /techan/dist/;
          index  index.html index.htm;
        }
       
        location ^~/api/ {
            proxy_pass http://101.200.242.66:8088/;
            proxy_set_header Host    $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

  注意:

location带/, proxy_pass不带上下文去除
# 转发示例:
# 请求:http://aaa.com/system/path/request
# 转发:http://bbb.com/path/request
# 
location ^~ /system/  {
    proxy_pass http://bbb.com;
}

保留
1. location不带/, proxy_pass不带上下文保留
# 转发示例
# 请求:http://aaa.com/system/path/request
# 转发:http://bbb.com/system/path/request
# 
location ^~ /system {
    proxy_pass http://bbb.com;
}

若proxy_pass 后加’/',代表去除掉请求和 location 的匹配的字符串
不加 ’ / ’ 则追加全部请求到地址后面。

  # 开启gzip
  gzip on;
  # 开启gzip_static
  # gzip_static 开启后可能会报错,需要安装相应的模块, 具体安装方式可以自行查询
  # 只有这个开启,vue文件打包的.gz文件才会有效果,否则不需要开启gzip进行打包
  gzip_static on;
  gzip_proxied any;
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  #如果nginx中使用了多层代理 必须设置这个才可以开启gzip。
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
  gzip_vary off;
  gzip_disable "MSIE [1-6]\.";

 

Nginx 常用命令

启动Nginx   start Nginx

关闭Nginx  Nginx -s stop

重启配置文件 nginx -s reload

快速停止或关闭Nginx:nginx -s stop

正常停止或关闭Nginx:nginx -s quit

 nginx部署多项目

参考:https://www.csdn.net/tags/MtjaYgzsNTU3OTItYmxvZwO0O0OO0O0O.html

https://blog.csdn.net/qq_18831501/article/details/123714516

user nginx lion # 用户是nginx;组是lion

worker_processes 4; # 指定具体子进程数量
worker_processes auto; # 与当前cpu物理核心数一致

域名匹配的四种写法:

精确匹配: server_name http://www.nginx.com ;
左侧通配: server_name *.http://nginx.com ;
右侧统配: server_name www.nginx.* ;
正则匹配: server_name ~^www\.nginx\.*$ ;
匹配优先级:精确匹配 > 左侧通配符匹配 > 右侧通配符匹配 > 正则表达式匹配

3、访问分析

当访问 http://www.nginx-test.com 时,都可以被匹配上,因此选择优先级最高的“完全匹配”;
当访问 http://mail.nginx-test.com 时,会进行“左匹配”;
当访问 http://www.nginx-test.org 时,会进行“右匹配”;
当访问 http://doc.nginx-test.com 时,会进行“左匹配”;
当访问 http://www.nginx-test.cn 时,会进行“右匹配”;
当访问 fe.nginx-test.club 时,会进行“正则匹配”;

root会将路径和url叠加,alias则只取定义路径。

location /image {
    alias /opt/nginx/static/image/;
}

#当用户访问 www.test.com/image/1.png 时,实际在服务器找的路径是 /opt/nginx/static/image/1.png

注意: 使用 alias 末尾一定要添加 / ,并且它只能位于 location 中。这个也是访问图片的配置方法

location匹配规则

location [ = | ~ | ~* | ^~ ] uri {
    ...
}

匹配规则:

= 精确匹配;
~ 正则匹配,区分大小写;
~* 正则匹配,不区分大小写;
^~ 匹配到即停止搜索;
匹配优先级: = > ^~ > ~ > ~* > 不带任何字符。

location的反斜杠

location /test {
    ...
}

location /test/ {
    ...
}

不带 / 当访问 http://www.nginx-test.com/test 时, Nginx 先找是否有 test 目录,如果有则找 test 目录下的 index.html ;如果没有 test 目录, nginx 则会找是否有 test 文件。
带 / 当访问 http://www.nginx-test.com/test 时, Nginx 先找是否有 test 目录,如果有则找 test 目录下的 index.html ,如果没有它也不会去找是否存在 test 文件。

autoindex

用户请求以 / 结尾时,列出目录结构,可以用于快速搭建静态资源下载网站。

server {
  listen 80;
  server_name fe.lion-test.club;
  
  location /download/ {
    root /opt/source;
    
    autoindex on; # 打开 autoindex,,可选参数有 on | off
    autoindex_exact_size on; # 修改为off,以KB、MB、GB显示文件大小,默认为on,以bytes显示出⽂件的确切⼤⼩
    autoindex_format html; # 以html的方式进行格式化,可选参数有 html | json | xml
    autoindex_localtime off; # 显示的⽂件时间为⽂件的服务器时间。默认为off,显示的⽂件时间为GMT时间
  }
}

负载均衡

hash算法通过指定关键字作为 hash key ,基于 hash 算法映射到特定的上游服务器中。关键字可以包含有变量、字符串。

upstream demo_server {
  hash $request_uri;
  server 121.42.11.34:8020;
  server 121.42.11.34:8030;
  server 121.42.11.34:8040;
}

server {
  listen 80;
  server_name balance.lion.club;
  
  location /balance/ {
      proxy_pass http://demo_server;
  }
}

ip_hash根据客户端的请求 ip 进行判断,只要 ip 地址不变就永远分配到同一台主机。它可以有效解决后台服务器 session 保持的问题

upstream demo_server {
  ip_hash;
  server 121.42.11.34:8020;
  server 121.42.11.34:8030;
  server 121.42.11.34:8040;
}

server {
  listen 80;
  server_name balance.lion.club;
  
  location /balance/ {
      proxy_pass http://demo_server;
  }
}

最少连接数:算法各个 worker 子进程通过读取共享内存的数据,来获取后端服务器的信息。来挑选一台当前已建立连接数最少的服务器进行分配请求。

upstream demo_server {
  zone test 10M; # zone可以设置共享内存空间的名字和大小
  least_conn;
  server 121.42.11.34:8020;
  server 121.42.11.34:8030;
  server 121.42.11.34:8040;
}

server {
  listen 80;
  server_name balance.lion.club;
  
  location /balance/ {
      proxy_pass http://demo_server;
  }
}

黑白名单

Nginx利用deny和allow指令来实现黑白名单的配置,利用黑白名单进行安全配置。

#语法
allow address | CIDR | all;
deny address | CIDR | all;

#模块:http/server/location
#参数说明:
#allow:允许访问。
#deny:禁止访问。
#address:具体的ip地址。
#CIDR:ip加掩码形式地址。
#all:所有ip地址。

配置禁止访问文件和文件夹

location ^~ /project/deny.txt {
alias /webroot/proj/;
deny all;
}

^~ /project/ 意思是接受从外部访问(如浏览器)的 URL 地址,比如http://www.domain.com/project;
^~ /project/deny.txt 意思是这一条 location 明确是对其起作用的;
alias /webroot/proj/ 意思是将 对 /project 的访问解析到 /webroot/proj 目录;
deny all 意思是屏蔽任何来源
也可以把 deny all 改换成 return 404,这样将返回 404 而不是 403 Forbidden,更有“欺骗性”。

 参考:https://zhuanlan.zhihu.com/p/368824600

posted @ 2018-06-20 15:28  刘百会  阅读(162)  评论(0编辑  收藏  举报