ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

dsg_16 nginx

2021-03-28 15:32:06  阅读:167  来源: 互联网

标签:http 16 -- root module nginx dsg ngx


nginx

nginx的作用

  • http web服务器
  • 反向代理服务器
  • mail代理服务器
  • 负载均衡(应用层)

nginx的特点

  • 高并发连接,nginx能够支撑3-5万并发连接
  • 内存消耗小,10个nginx进程会消耗150M内存
  • 成本低廉

nginx安装

nginx下载

nginx官网
在这里插入图片描述

nginx安装

	#1. 安装之前需要先安装以下环境
	#a. 查看是否安装了gcc
	[root@Centos100 nginx]# yum list installed | grep gcc
	gcc.x86_64                            4.8.5-44.el7                     @base    
	gcc-c++.x86_64                        4.8.5-44.el7                     @base    
	libgcc.x86_64                         4.8.5-44.el7                     @base
	#如果有上面的结果,表示安装了,否则执行以下命令安装gcc
	[root@Centos100 nginx]# yum -y install gcc gcc-c++ autoconf automake
	#b. 查看是否安装了zlib
	[root@Centos100 nginx]# yum list installed | grep zlib
	zlib.x86_64                           1.2.7-17.el7                     @anaconda
	#如果有上面的结果,表示安装了,否则执行以下命令安装zlib
	[root@Centos100 nginx]# yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
	#c. 查看是否安装了pcre-devel
	[root@Centos100 nginx]# yum list installed | grep pcre-devel
	pcre-devel.x86_64                     8.32-17.el7                      @base
	#如果有上面的结果,表示安装了,否则执行以下命令安装pcre-devel
	[root@Centos100 nginx]# yum -y install pcre-devel
	#d. 查看是否安装了openssl-devel
	[root@Centos100 nginx]# yum list installed | grep openssl
	openssl.x86_64                        1:1.0.2k-21.el7_9                @updates 
	openssl-devel.x86_64                  1:1.0.2k-21.el7_9                @updates 
	openssl-libs.x86_64                   1:1.0.2k-21.el7_9                @updates
	[root@Centos100 nginx]# yum -y install openssl openssl-devel
	
	#2. tar开
	[root@Centos100 nginx]# tar -zvxf nginx-1.18.0.tar.gz

	#3. 到nginx-1.18.0目录下,查看帮助,如下面所示
	[root@Centos100 nginx]# cd nginx-1.18.0
	[root@Centos100 nginx-1.18.0]# ./configure --help

	#4. 设置安装路径,选择安装的功能模块,然后编译,\表示换行,方便复制到命令行中
	[root@Centos100 nginx-1.18.0]# ./configure\
	 --prefix=/software/nginx/nginx\
	 --sbin-path=/software/nginx/nginx/sbin/nginx\
	 --modules-path=/software/nginx/nginx/modules\
	 --conf-path=/software/nginx/nginx/conf/nginx.conf\
	 --error-log-path=/software/nginx/nginx/log/error-log.out\
	 --pid-path=/software/nginx/nginx/pid\
	 --lock-path=/software/nginx/nginx/lock\
	 --user=root\
	 --group=root\
	 --with-http_ssl_module\
	 --with-http_flv_module\
	 --with-http_gzip_static_module\
	 --http-log-path=/software/nginx/nginx/log/http-log.out\
	 --http-client-body-temp-path=/software/nginx/nginx/http-client-body-temp\
	 --http-proxy-temp-path=/software/nginx/nginx/http-proxy-temp\
	 --http-fastcgi-temp-path=/software/nginx/nginx/http-fastcgi-temp\
	 --with-http_stub_status_module
	Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  #编译执行后最后会出现下面的结果
  nginx path prefix: "/software/nginx/nginx"
  nginx binary file: "/software/nginx/nginx/sbin/nginx"
  nginx modules path: "/software/nginx/nginx/modules"
  nginx configuration prefix: "/software/nginx/nginx"
  nginx configuration file: "/software/nginx/nginx/nginx-conf.conf"
  nginx pid file: "/software/nginx/nginx/pid"
  nginx error log file: "/software/nginx/nginx/log/error-log.out"
  nginx http access log file: "/software/nginx/nginx/log/http-log.out"
  nginx http client request body temporary files: "/software/nginx/nginx/http-client-body-temp"
  nginx http proxy temporary files: "/software/nginx/nginx/http-proxy-temp"
  nginx http fastcgi temporary files: "/software/nginx/nginx/http-fastcgi-temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

 #5. 编译后出现上面的结果表示编译成功!,成功后执行下面的命令,安装nginx
 [root@Centos100 nginx-1.18.0]# make && make install

nginx编译安装帮助文档:


  --help                             print this message

  --prefix=PATH                      set installation prefix(设置安装路径)
  --sbin-path=PATH                   set nginx binary pathname(设置nginx执行文件的路径和文件名称)
  --modules-path=PATH                set modules path(设置模块路径)
  --conf-path=PATH                   set nginx.conf pathname(设置nginx配置文件的路径和文件名称)
  --error-log-path=PATH              set error log pathname(设置错误日志文件的路径和文件名称)
  --pid-path=PATH                    set nginx.pid pathname(设置nginx.pid进程文件的路径)
  --lock-path=PATH                   set nginx.lock pathname(设置nginx.lock锁文件的路径)

  --user=USER                        set non-privileged user for
                                     worker processes(指定linux用户)
  --group=GROUP                      set non-privileged group for
                                     worker processes(指定linux组)

  --build=NAME                       set build name 
  --builddir=DIR                     set build directory

  # 功能模块,without表示默认安装的功能模块(你想要去掉他可以使用without去掉),with表示可选择安装的功能模块,不是默认的(你想用它可以使用with去安装)
  --with-select_module               enable select module
  --without-select_module            disable select module
  --with-poll_module                 enable poll module
  --without-poll_module              disable poll module

  --with-threads                     enable thread pool support

  --with-file-aio                    enable file AIO support

  --with-http_ssl_module             enable ngx_http_ssl_module
  --with-http_v2_module              enable ngx_http_v2_module
  --with-http_realip_module          enable ngx_http_realip_module
  --with-http_addition_module        enable ngx_http_addition_module
  --with-http_xslt_module            enable ngx_http_xslt_module
  --with-http_xslt_module=dynamic    enable dynamic ngx_http_xslt_module
  --with-http_image_filter_module    enable ngx_http_image_filter_module
  --with-http_image_filter_module=dynamic
                                     enable dynamic ngx_http_image_filter_module
  --with-http_geoip_module           enable ngx_http_geoip_module
  --with-http_geoip_module=dynamic   enable dynamic ngx_http_geoip_module
  --with-http_sub_module             enable ngx_http_sub_module
  --with-http_dav_module             enable ngx_http_dav_module
  --with-http_flv_module             enable ngx_http_flv_module(流媒体)
  --with-http_mp4_module             enable ngx_http_mp4_module
  --with-http_gunzip_module          enable ngx_http_gunzip_module
  --with-http_gzip_static_module     enable ngx_http_gzip_static_module(压缩功能)
  --with-http_auth_request_module    enable ngx_http_auth_request_module
  --with-http_random_index_module    enable ngx_http_random_index_module
  --with-http_secure_link_module     enable ngx_http_secure_link_module
  --with-http_degradation_module     enable ngx_http_degradation_module
  --with-http_slice_module           enable ngx_http_slice_module
  --with-http_stub_status_module     enable ngx_http_stub_status_module(启用http状态查询功能)

  --without-http_charset_module      disable ngx_http_charset_module
  --without-http_gzip_module         disable ngx_http_gzip_module
  --without-http_ssi_module          disable ngx_http_ssi_module
  --without-http_userid_module       disable ngx_http_userid_module
  --without-http_access_module       disable ngx_http_access_module
  --without-http_auth_basic_module   disable ngx_http_auth_basic_module
  --without-http_mirror_module       disable ngx_http_mirror_module
  --without-http_autoindex_module    disable ngx_http_autoindex_module
  --without-http_geo_module          disable ngx_http_geo_module
  --without-http_map_module          disable ngx_http_map_module
  --without-http_split_clients_module disable ngx_http_split_clients_module
  --without-http_referer_module      disable ngx_http_referer_module
  --without-http_rewrite_module      disable ngx_http_rewrite_module
  --without-http_proxy_module        disable ngx_http_proxy_module
  --without-http_fastcgi_module      disable ngx_http_fastcgi_module
  --without-http_uwsgi_module        disable ngx_http_uwsgi_module
  --without-http_scgi_module         disable ngx_http_scgi_module
  --without-http_grpc_module         disable ngx_http_grpc_module
  --without-http_memcached_module    disable ngx_http_memcached_module
  --without-http_limit_conn_module   disable ngx_http_limit_conn_module
  --without-http_limit_req_module    disable ngx_http_limit_req_module
  --without-http_empty_gif_module    disable ngx_http_empty_gif_module
  --without-http_browser_module      disable ngx_http_browser_module
  --without-http_upstream_hash_module
                                     disable ngx_http_upstream_hash_module
  --without-http_upstream_ip_hash_module
                                     disable ngx_http_upstream_ip_hash_module
  --without-http_upstream_least_conn_module
                                     disable ngx_http_upstream_least_conn_module
  --without-http_upstream_random_module
                                     disable ngx_http_upstream_random_module
  --without-http_upstream_keepalive_module
                                     disable ngx_http_upstream_keepalive_module
  --without-http_upstream_zone_module
                                     disable ngx_http_upstream_zone_module

  --with-http_perl_module            enable ngx_http_perl_module
  --with-http_perl_module=dynamic    enable dynamic ngx_http_perl_module
  --with-perl_modules_path=PATH      set Perl modules path
  --with-perl=PATH                   set perl binary pathname

  --http-log-path=PATH               set http access log pathname
  --http-client-body-temp-path=PATH  set path to store
                                     http client request body temporary files
  --http-proxy-temp-path=PATH        set path to store
                                     http proxy temporary files(设置代理的临时目录)
  --http-fastcgi-temp-path=PATH      set path to store
                                     http fastcgi temporary files
  --http-uwsgi-temp-path=PATH        set path to store
                                     http uwsgi temporary files
  --http-scgi-temp-path=PATH         set path to store
                                     http scgi temporary files

  --without-http                     disable HTTP server
  --without-http-cache               disable HTTP cache

  --with-mail                        enable POP3/IMAP4/SMTP proxy module
  --with-mail=dynamic                enable dynamic POP3/IMAP4/SMTP proxy module
  --with-mail_ssl_module             enable ngx_mail_ssl_module
  --without-mail_pop3_module         disable ngx_mail_pop3_module
  --without-mail_imap_module         disable ngx_mail_imap_module
  --without-mail_smtp_module         disable ngx_mail_smtp_module

  --with-stream                      enable TCP/UDP proxy module
  --with-stream=dynamic              enable dynamic TCP/UDP proxy module
  --with-stream_ssl_module           enable ngx_stream_ssl_module
  --with-stream_realip_module        enable ngx_stream_realip_module
  --with-stream_geoip_module         enable ngx_stream_geoip_module
  --with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module
  --with-stream_ssl_preread_module   enable ngx_stream_ssl_preread_module
  --without-stream_limit_conn_module disable ngx_stream_limit_conn_module
  --without-stream_access_module     disable ngx_stream_access_module
  --without-stream_geo_module        disable ngx_stream_geo_module
  --without-stream_map_module        disable ngx_stream_map_module
  --without-stream_split_clients_module
                                     disable ngx_stream_split_clients_module
  --without-stream_return_module     disable ngx_stream_return_module
  --without-stream_upstream_hash_module
                                     disable ngx_stream_upstream_hash_module
  --without-stream_upstream_least_conn_module
                                     disable ngx_stream_upstream_least_conn_module
  --without-stream_upstream_random_module
                                     disable ngx_stream_upstream_random_module
  --without-stream_upstream_zone_module
                                     disable ngx_stream_upstream_zone_module

  --with-google_perftools_module     enable ngx_google_perftools_module
  --with-cpp_test_module             enable ngx_cpp_test_module

  --add-module=PATH                  enable external module
  --add-dynamic-module=PATH          enable dynamic external module

  --with-compat                      dynamic modules compatibility

  --with-cc=PATH                     set C compiler pathname
  --with-cpp=PATH                    set C preprocessor pathname
  --with-cc-opt=OPTIONS              set additional C compiler options
  --with-ld-opt=OPTIONS              set additional linker options
  --with-cpu-opt=CPU                 build for the specified CPU, valid values:
                                     pentium, pentiumpro, pentium3, pentium4,
                                     athlon, opteron, sparc32, sparc64, ppc64

  --without-pcre                     disable PCRE library usage
  --with-pcre                        force PCRE library usage
  --with-pcre=DIR                    set path to PCRE library sources
  --with-pcre-opt=OPTIONS            set additional build options for PCRE
  --with-pcre-jit                    build PCRE with JIT compilation support

  --with-zlib=DIR                    set path to zlib library sources
  --with-zlib-opt=OPTIONS            set additional build options for zlib
  --with-zlib-asm=CPU                use zlib assembler sources optimized
                                     for the specified CPU, valid values:
                                     pentium, pentiumpro

  --with-libatomic                   force libatomic_ops library usage
  --with-libatomic=DIR               set path to libatomic_ops library sources

  --with-openssl=DIR                 set path to OpenSSL library sources
  --with-openssl-opt=OPTIONS         set additional build options for OpenSSL

  --with-debug                       enable debug logging

nginx启动

#1. 到nginx的安装目录
[root@Centos100 nginx-1.18.0]# cd /software/nginx/nginx/sbin
#2. 启动nginx
[root@Centos100 sbin]# ./nginx -c ../conf/nginx.conf
#3. 查看nginx进程
[root@Centos100 sbin]# ps aux | grep nginx
root     13240  0.0  0.1  45984  1124 ?        Ss   16:35   0:00 nginx: master process ./nginx -c nginx-conf.conf
root     13241  0.0  0.1  46424  1888 ?        S    16:35   0:00 nginx: worker process
root     13243  0.0  0.0 112828   968 pts/0    S+   16:35   0:00 grep --color=auto nginx
#4. 查看80端口
[root@Centos100 sbin]# netstat -tulnp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      13240/nginx: master
#5. 关闭nginx进程
[root@Centos100 sbin]# kill -9 13240
#6. 重启nginx进程
[root@Centos100 sbin]# kill -1 13240
#7. 检测配置文件语法
[root@Centos100 sbin]# ./nginx -t -c nginx-conf.conf

启动成功后在页面输入ip直接访问查看
在这里插入图片描述

nginx配置

#user  nobody;
#1. 配置nginx工作进程数,nginx推荐有几个cpu,就配置几个进程
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

#2. 设置并发的最大连接数
events {
    worker_connections  1024;
}

#3. http服务配置
http {
#4. http服务全局配置
    include       mime.types;
    default_type  application/octet-stream;
	
	# 定义日志格式,和代表这个格式的名称,后面是日志格式:请求地址-请求用户[请求时间] "request" 请求状态码
    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;
    #保持连接存活时间:65秒
    keepalive_timeout  65;
	#是否启用压缩功能,默认关闭
    gzip  on;
#4. server服务配置,一个http服务下面可以配置多个server服务
    server {
    	#监听端口,后面加default_server表示为默认服务
        listen       80 default_server;
        #配置服务名称,多个服务名称用空格隔开
        server_name  localhost;

        #charset koi8-r;

		#设置日志路径,注意路径是绝对路径,跟root设置路径不一样
		#access_log记录所有日志,并且按照设置的日志格式来记录日志
        access_log /software/nginx/nginx/software/nginx/nginx/applicationLog/80AccessLogs.log  main;
        #error_log记录设置的级别的日志,nginx有“stderr”, “emerg”, “alert”, “crit”, “error”,”warn”, “notice”, “info”, “debug”这几种级别,每次只能取其中一个
        error_log /software/nginx/nginx/software/nginx/nginx/applicationLog/80ErrorLogs.log  warn;
		

#5. 一个server里面可以定义多个location
        location / {
        	#配置web应用所在的目录,该目录以nginx的安装目录开始计算,root也可以写到server里面去
            root   html;
            #配置默认访问的文件
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        # 访问出错,500,502,503,504跳的页面
        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;
    #    }
    #}

}

查看服务器cpu个数

# 查看cpu
[root@Centos100 sbin]# cat /proc/cpuinfo
cpu cores	: 2

location的配置

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

#只匹配 / 的请求
location = / {
}

#只匹配以 / 开始的请求
location / {
}

#只匹配以 /images/ 开始的请求,并停止搜素,不检查正则表达式
location ^~ /images/ {
}

#匹配任何以gif或者jpg或者jpeg结尾的请求,$表示以某某某为结尾的标识
location ~* \.(gif|jpg|jpeg)${
}
  • = 精确匹配
  • ~ 区分大小写
  • ~* 不区分大小写
  • ^~ 禁止表达式匹配

匹配规则: location匹配的优先级是选择更加精确的匹配,比如上面例子里最后两个,图片请求如果是以images开头并且是jpg结尾,那他会匹配前面一个请求,而不会匹配后面请求,因为前面的以images开头的请求范围更小,更加精确

自定义错误页面

#server里面定义
#当访问出现500,502,503,504的状态码时,他会给你重定向到http://Centos100/50x.html,这时候根据请求他就会匹配到下面的location然后进入我们的自定义错误页面
error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   html;
}

配置索引文件

location = /data {
	root file;
	autoindex on;
}

创建链接

有的文件路径太长,会造成访问链接太长,为了便于访问,可以使用以下方法设置

location /image {
	#这里是绝对路径,不是从nginx安装目录开始计算,跟root设置不一样
	alias /software/nginx/nginx/file/data/bb;
}

控制站点访问

location /image {
	alias /software/nginx/nginx/file/data/bb;
	allow 192.168.113.1;
	deny 192.168.113.2;
	deny all;
}

控制站点的优先级是按照顺序来的,也就是说如果在前面允许了该ip地址访问,那么即使下面有设置拒绝该ip访问的,这个ip还是可以访问这个链接的,而默认情况下是全部都允许的,所以我们在最后加上deny all可以屏蔽所有除了上面允许访问的ip,保证系统的最大安全性。

身份验证

ngx_http_auth_basic_module模块实现访问必须输入用户名和密码

#a. 安装htpasswd
#查看是否安装了htpasswd
[root@Centos100 nginx]# yum list installed | grep httpd-tools
httpd-tools.x86_64                    2.4.6-97.el7.centos              @updates
# 如果没有,安装
[root@Centos100 nginx]# yum install -y httpd-tools
#b. 生成用户密码文件htpasswd.users,并添加用admin和密码1;参数b:添加用户,c:生成用户密码文件
[root@Centos100 nginx]# htpasswd -bc /software/nginx/nginx/file/users/htpasswd.users admin 1
#c. 添加新用户zhangsan,密码1;参数b:添加用;如果添加已经存在的用户,则是修改密码
[root@Centos100 nginx]# htpasswd -b /software/nginx/nginx/file/users/htpasswd.users zhangsan 1
#d. 删除用户;参数D:删除用户
[root@Centos100 nginx]# htpasswd -D /software/nginx/nginx/file/users/htpasswd.users zhangsan
#e. 修改nginx配置文件
location /image {
	alias /software/nginx/nginx/file/data/bb;
	#这两个属性也可以配置到server中去,控制全局
	auth_basic "Restricted Access";
    auth_basic_user_file /software/nginx/nginx/file/users/htpasswd.users;
	allow 192.168.113.1;
	deny 192.168.113.2;
	deny all;
}

nginx状态监测

http_stub_status_module模块可以实现nginx的状态监测

#状态监测,nginx_status这个目录可以不存在,并且可以不用指定root
location /nginx_status {
	#开启状态监测
	stub_status on;
	#关闭访问这个链接的日志输出
	access_log off;
}

在这里插入图片描述

nginx地址重写

nginx 预定义的变量

变量名称含义
$arg_PARAMETER这个变量值为:GET请求中变量名PARAMETER参数的值。
$args这个变量等于GET请求中的参数。例如,foo=123&bar=blahblah;这个变量只可以被修改
$binary_remote_addr二进制码形式的客户端地址。
$body_bytes_sent传送页面的字节数
$content_length请求头中的Content-length字段。
$content_type请求头中的Content-Type字段。
$cookie_COOKIEcookie COOKIE的值。
$document_root当前请求在root指令中指定的值。
$document_uri与$uri相同。
$host请求中的主机头(Host)字段,如果请求中的主机头不可用或者空,则为处理请求的server名称(处理请求的server的server_name指令的值)。值为小写,不包含端口。
hostname机器名使用 gethostname系统调用的值
$http_HEADERHTTP请求头中的内容,HEADER为HTTP请求中的内容转为小写,-变为_(破折号变为下划线),例如:$http_user_agent(Uaer-Agent的值), $http_referer…;
$sent_http_HEADERHTTP响应头中的内容,HEADER为HTTP响应中的内容转为小写,-变为_(破折号变为下划线),例如: $sent_http_cache_control, $sent_http_content_type…;
$is_args如果$args设置,值为"?",否则为""。
$limit_rate这个变量可以限制连接速率。
$nginx_version当前运行的nginx版本号。
$query_string与$args相同。
$remote_addr客户端的IP地址。
$remote_port客户端的端口。
$remote_user已经经过Auth Basic Module验证的用户名。
$request_filename当前连接请求的文件路径,由root或alias指令与URI请求生成。
$request_body这个变量(0.7.58+)包含请求的主要信息。在使用proxy_pass或fastcgi_pass指令的location中比较有意义。
$request_body_file客户端请求主体信息的临时文件名。
$request_completion如果请求成功,设为"OK";如果请求未完成或者不是一系列请求中最后一部分则设为空。
$request_method这个变量是客户端请求的动作,通常为GET或POST。
$request_uri这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看$uri更改或重写URI。
$scheme所用的协议,比如http或者是https,比如rewrite ^(.+)$ $scheme://example.com$1 redirect;
$server_addr服务器地址,在完成一次系统调用后可以确定这个值,如果要绕开系统调用,则必须在listen中指定地址并且使用bind参数。
$server_name服务器名称。
$server_port请求到达服务器的端口号。
$server_protocol请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$uri请求中的当前URI(不带请求参数,参数位于 a r g s ) , 不 同 于 浏 览 器 传 递 的 args),不同于浏览器传递的 args),不同于浏览器传递的request_uri的值,它可以通过内部重定向,或者使用index指令进行修改。不包括协议和主机名,例如/foo/bar.html

if 指令

#如果请求的浏览器是MSIE,那么就重写地址
#注意:if和(之间要有空格
if ($http_user_agent ~ MSIE){
	#^:以某某某开始;.:一个字符;.*:任意字符串;$:以某某某结尾
	#break:rewrite的flag标记
	#将^(.*)$匹配的uri转向/msie/$1链接上去
	rewrite ^(.*)$ /msie/$1 break;
}

#如果请求找不到这个文件,那么就重写地址
if (!-f $request_filename){
	#将^/img/(.*)$匹配的uri转到/site/$host/images/$1目录下
	#last rewrite的flag标记
	rewrite ^/img/(.*)$ /site/$host/images/$1 last;
}

set、rewrite指令

#如果请求地址是以任意字符开头,以.aaa.com结尾,不区分大小写,设置变量var_tz=1
if($host ~* ^(.*?)\.aaa\.com$){
	set $var_tz '1';
}
#如果请求地址是以192.168.1.开头,以任意字符结尾的,不区分大小写,设置变量var_tz=1
if($host ~* ^192\.168\.1\.(.*?)$){
	set $var_tz '1';
}
#如果请求地址是以localhost开头的,设置变量var_tz=1
if($host ~* ^localhost){
	set $var_tz '1';
}
#如果变量var_tz不等于'1',区分大小写,则重写地址到http://www.aaa.com/,并设置rewrite的flag标记为redirect
if($var_tz !~ '1'){
	rewrite ^/(.*)$ http://www.aaa.com/ redirect;
}

rewrite指令flag标记

  • last:实现uri重写,浏览器url地址栏不变,还会继续匹配后面的规则
  • break:实现uri重写,浏览器url地址栏不变,并且不再匹配后面的规则
  • redirect:实现uri重写,浏览器url地址栏显示跳转的地址,并且请求返回状态码302临时重定向
  • permanent:实现uri重写,浏览器url地址栏显示跳转的地址,并且请求返回状态码301永久重定向

注意:一般精确匹配的用break标记,在大范围比如在service或者localtion / {}匹配的用last
demo:

	#1. 将要访问的data目录重写到bbs上去
	location /data {
	    root file;
	    autoindex on;
	    rewrite ^/data/?$ /bbs/ permanent;
	}

	location /bbs {
	    root file;
	    autoindex on;
	}
	#2. 根据不同的浏览器得到不同的结果
	root /software/nginx/nginx/file;
	if ( $http_user_agent ~ MSIE ){
         rewrite ^(.*)$ /msie/$1 break;
    }
    if ( $http_user_agent ~ Chrome ){
         rewrite ^(.*)$ /chrome/$1 break;
    }

反向代理

server {
	listen 90;
	server_name localhost;
	location / {
		#在地址后面加个斜杠,这样请求的/后面的uri就会拼到自定义的服务后面去
	   proxy_pass http://centos101:8080/;
	}
}

代理kafka服务

#如果没有stream模块,则需要给nginx添加stream模块
[root@localhost nginx-1.12.2]# ./configure --with-stream
[root@localhost nginx-1.12.2]# make & make install

#修改配置文件
stream{
	#需要代理的kafka集群
    upstream brokers{
        server 192.168.4.31:18001;
        server 192.168.4.65:18001;
        server 192.168.4.68:18001;
    }
    server{
    	#指定端口
        listen 18001;
        proxy_pass brokers;
    }
}

负载均衡

http{
	#自定义服务使用upstream定义,并且要在http下面定义
	upstream myServer {
		#weight:权重;max_fails:最大失败次数;fail_timeout:超时时间
		server centos101:8080 weight=1 max_fails=2 fail_timeout=30s;
		server centos102:8080 weight=2 max_fails=2 fail_timeout=30s;
   	}
   	server {
		listen 90;
		server_name localhost;
		location /demo {
			#在地址后面加个斜杠,这样请求的demo后面的uri就会拼到自定义的服务后面去
		   proxy_pass http://myServer/demo/;
		}
	}
}

nginx日志管理

log_format指令用来设置日志的记录格式,语法如下:

log_format name 'format'
#具体日志格式如下:
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"';
#$remote_addr用于记录ip地址
#$remote_user用来记录远程客户端用户名称
#$time_local用来记录访问时间及时区
#$request用于记录请求的URL与HTTP协议
#$status用于记录请求的状态,例如成功时状态为200,页面找不到时为404$body_bytes_sent用于记录发送给客户端的文件主体内容大小
#$body_bytes_sent用于记录传送页面的字节数
#$http_referer用于记录是从哪个页面链接访问过来的
#$http_user_agent用于记录客户端浏览器的相关信息

#日志
192.168.113.1 - - [30/Jan/2021:22:58:56 +0800] "GET /index.html HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36" "-"

限速设置

location /data {
	root file;
	#当他下载达到20m的时候限速
	limit_rate_after 20m; 
	#限速128kb/s
	limit_rate 128k;
}

标签:http,16,--,root,module,nginx,dsg,ngx
来源: https://blog.csdn.net/qq_34191426/article/details/113354381

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有