• Nginx入门(二):常用功能配置


    1.开始

    进入nginx的安装目录,我的是在/etc/nginx,会有一个默认的nginx.config配置文件,里面已经包含基本配置,并且设置了默认扫描/etc/nginx/conf.d/目录下所有以conf结尾的文件,也就是说,如果有自己需要的配置,直接创一个新.conf的文件即可。

    我们先不进行其他配置,第一次启动nginx需要制定配置文件位置:

    nginx -c /etc/nginx/nginx.conf
    

    然后访问网站的80端口即可!

    如果中途报错,就会用到最常用的操作,也就是查看日志:

    tail -f /var/log/nginx/access.log
    

    2.配置文件分析(注意分号结尾)

    user  nginx;
    #工作进程,配置与cpu个数保持一致
    worker_processes  1;
    
    #日志打印级别,现在是warn
    error_log  /var/log/nginx/error.log warn;
    #服务器启动后的pid
    pid        /var/run/nginx.pid;
    
    #事件模块
    events {
    	#每个worker支持的请求数量
        worker_connections  1024;
        #内核模型(select,poll,epoll)
        use epool;
    }
    
    #非虚拟主机的配置
    http {
        include       /etc/nginx/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  /var/log/nginx/access.log  main;
    		
    		#虚拟主机的配置,每个虚拟主机配置一个server
            server{
            		#监听端口,默认80
            		listen 80;
            		#提供服务的域名或者主机名
                    server_name www.xxxxx.top;
                    #'/'后面可以匹配规则,比如location /1.html{}
                    location / {
                    	#网站的存放路径
                    	root /soft/code/www;
                    	#默认访问的首页 
                    	index index.html;
                    }
                    #错误页面统一定位
                    error_page 500 502 503 504 /50x.html
                    #错误代码重新定向到新的location
                    location = /50x.html{
                    	root html
                    }
                    
                    #include可以在当前标签内引用外部配置文件,等同于直接写在include所在位置
                    include /usr/local/nginx/conf/vhost/*;
            }
    
        sendfile        on;
        #tcp_nopush     on;
    
        keepalive_timeout  65;
    
        #gzip  on;
        
    
    "/etc/nginx/nginx.conf" 38L, 735C   
    

    3.测试运行nginx的配置文件

    如果更改了配置,就需要进行以下操作以让nginx重新加载配置并启动

    #一次启动nginx需要指定配置文件位置
    nginx -c /etc/nginx/nginx.conf
    
    #若在默认目录则不需要指定位置
    nginx -t
    
    #重新加载配置文件
    nginx -s reload/restart
    

    我们可以在启动后直接通过curl在linux上查看访问结果:

    curl -v xxxxxxx.top
    

    4.配置日志服务

    我们可以配置日志可视化页面,通过访问固定的地址以获得nginx的运行情况

    4.1 配置页面

    #在server下配置,然后在地址栏访问网址/mystatus即可
    location /mystatus{
            stub_status on;
            access_log off;
    }
    
    

    4.2 页面信息

    以下为访问的http://xxxxxx.top/mystatus页面的展示信息

    #当前nginx活跃的连接数
    Active connections: 2 
    
    #server表示启动到现在处理的总连接数
    #accepts表示启动到现在一共建立了几次握手
    #另外,总连接数减去握手数则为请求丢失数
    #handled requests表示总共处理的请求数
    server accepts handled requests
     18 18 17 
    
    #reading表示读取到客户端header信息数
    #writing表示返回给客户端的header信息数
    #waiting表示开启keep-alive长连接情况下,没有读写而建立长链接的情况
    Reading: 0 Writing: 1 Waiting: 1 
    

    5.配置下载服务

    我们可以配置下载服务,通过指定服务器的某个文件夹,让这个文件夹中的文件可以通过nginx直接从浏览器上被下载

    location /down{
    	#配置文件夹位置
    	#注意:这里实际访问的是/soft/package/src/down这个文件夹
        root /soft/package/src;
        #显示目录
        autoindex on;
        #显示上传时间
        autoindex_localtime on;
        #显示文件大概大小
        autoindex_exact_size off;
    }
    

    6.配置访问限制和请求限制

    对于频繁请求,我们可以通过nginx做出限制,以防止被占用过多的服务器资源。

    参考:https://www.cnblogs.com/shouke/p/10157552.html

    注:多个请求可能建立在一个TCP连接上,因此限制请求比限制连接有效

    6.1 声明配置

    #在http模块中全局定义请求限制,然后再在location中引用配置
    limit_req_zone $binary_remote_addr zone=req_zone:1m rate=1r/s;
    

    6.2 配置页面

    location / {
        root /soft/code/www;
        index index.html;
    	#在location中引用配置
    	#只接受一个请求,多余的直接处理
        #limit_req zone=req_zone;
    	#只接受一个请求,多余的延迟处理,当请求数多余burst,则多余的返回503
        limit_req zone=req_zone burst=3 nodelay;
    
    }
    

    6.3 访问限制

    #在location中配置
    #deny表示拒绝访问,可以为具体ip或all
    #allow表示允许访问
    deny 222.173.61.94;
    deny 39.128.149.179;
    allow all;
    

    注意:拒绝跟允许有逻辑顺序,两者上下位置在运行的时候会产生影响

    例如:deny all;
    allow 222.173.61.94;

    ​ 会拒绝所有访问

    7.登录认证

    我们可以通过nginx对用户的请求做登入认证拦截。

    7.1 安装httpd-tools

    [root@iZwz9ev02los7q1d71e7muZ nginx]# yum install httpd-tools
    

    7.2 htpasswd说明

    [root@iZwz9ev02los7q1d71e7muZ nginx]# htpasswd --help
    Usage:
    	htpasswd [-cimBdpsDv] [-C cost] passwordfile username
    	htpasswd -b[cmBdpsDv] [-C cost] passwordfile username password
    
    	htpasswd -n[imBdps] [-C cost] username
    	htpasswd -nb[mBdps] [-C cost] username password
    	#直接创建新文件会导致旧文件被覆盖
     -c  Create a new file.
     -n  Don't update file; display results on stdout.
     -b  Use the password from the command line rather than prompting for it.
     -i  Read password from stdin without verification (for script usage).
     -m  Force MD5 encryption of the password (default).
     -B  Force bcrypt encryption of the password (very secure).
     -C  Set the computing time used for the bcrypt algorithm
         (higher is more secure but slower, default: 5, valid: 4 to 31).
     -d  Force CRYPT encryption of the password (8 chars max, insecure).
     -s  Force SHA encryption of the password (insecure).
     	#使用明文密码
     -p  Do not encrypt the password (plaintext, insecure).
     	#删除指定用户
     -D  Delete the specified user.
     -v  Verify password for the specified user.
    On other systems than Windows and NetWare the '-p' flag will probably not work.
    The SHA algorithm does not use a salt and is less secure than the MD5 algorithm.
    

    7.3 htpasswd使用

    注意:不指明目录则默认在当前目录生成.htpasswd文件

    7.3.1 设置配置文件,添加用户

    [root@iZwz9ev02los7q1d71e7muZ /]# htpasswd -c /etc/nginx/auth_conf huang
    #若已有配置文件,想要不要覆盖旧的并且添加新用户
    htpasswd -b /etc/nginx/auth_conf 用户名 密码
    
    #输入密码回车显示添加了新用户
    New password: xxxxxxx
    Re-type new password: xxxxxxx
    

    7.3.2 确认配置文件

    [root@iZwz9ev02los7q1d71e7muZ /]# cat /etc/nginx/auth_conf
    
    #显示账号和密码
    xxx:$apr1$u6cnrOVn$tlA7/I8CMyng.zEYrMr1W.
    

    7.3.3 在loaction中启用模块

    #当访问时提示消息
    auth_basic "please login!";
    #指明配置文件位置
    auth_basic_user_file /etc/nginx/auth_conf;
    

    当再次访问的时候,就会要求输入账号和密码了!

    8.配置静态资源服务

    我们可以通过nginx配置一个静态资源服务器,当请求获取某个文件的时候,nginx可以通过匹配文件后缀,自动去指定文件夹中获取文件

    8.1 在location里配置拦截特定后缀

    #根据后缀拦截,这里拦截的是图片
    location ~ .*.(gif|GIF|jpg|JPG|jpeg|JEPG|png|PNG)$ {
                    gzip on;
                    gzip_http_version 1.1;
                    gzip_comp_level 2;
                    gzip_types text/plain application/json application/x-javascript application/css application/xml application/xml+rss text/javascript     application/x-httpd-php image/jpeg image/gif image/png;
    				#配置静态资源库
                    root /soft/package/img;
     }
    
    

    8.2 对图片传输配置

    #配置gzip
    
    #开启压缩
    gzip on;
    #http协议版本
    gzip_http_version 1.1;
    #压缩等级
    gzip_comp_level 2;
    #压缩的文件类型
    gzip_types image/gif image/jpeg image/png image/svg+xml image/tiff image/vnd.wap.wbmp image/webp image/x-icon image/x-jng image/x-ms-bmp
    

    注意:gzip_type这一栏可以在nginx安装目录里找到所有类型

    [root@iZwz9ev02los7q1d71e7muZ package]# vim /etc/nginx/mime.types
    
    
    types {
        text/html                                        html htm shtml;
        text/css                                         css;
        text/xml                                         xml;
        image/gif                                        gif;
        image/jpeg                                       jpeg jpg;
        application/javascript                           js;
        application/atom+xml                             atom;
        application/rss+xml                              rss;
    
        text/mathml                                      mml;
        text/plain                                       txt;
        text/vnd.sun.j2me.app-descriptor                 jad;
        text/vnd.wap.wml                                 wml;
        text/x-component                                 htc;
    
        image/png                                        png;
        image/svg+xml                                    svg svgz;
        image/tiff                                       tif tiff;
        image/vnd.wap.wbmp                               wbmp;
        image/webp                                       webp;
        image/x-icon                                     ico;
        image/x-jng                                      jng;
        image/x-ms-bmp                                   bmp;
    							... ...
    

    8.3 设置防盗链

    #在对应location中判断是否为允许访问的ip/域名
    valid_referers none blocked 111.17.194.89;
    #如果不是,就返回一个403
    #注意:if和()之间需要空格
    if ($invalid_referer){
        return 403;
    }
    

    注:

    资源服务器可以通过网易或七牛cdn之类的服务商白嫖,不一定用自己的,毕竟自己的破服务器流量和性能都有限。

    详情可以参考之前那篇typora使用七牛云做图床的文章。

    9.反向代理

    这个可能是nginx最常用的功能了,通过设置端口代理,我们可以把对某个端口的请求代理到另一个端口,比如最常见的80转8080

    9.1 配置页面

    #在location中添加
    location / {
    	#将默认地址的请求转到http://www.qiuyeye.cn/index.html去
        proxy_pass http://www.qiuyeye.cn/index.html;
        include /etc/nginx/conf.d/proxy_params.conf;
    }
    

    9.2 配置参数

    #添加代理参数
    proxy_redirect  default;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
    proxy_connect_timeout 30;
    proxy_send_timeout 60;
    proxy_read_timeout 60;
    
    proxy_buffer_size 32k;
    proxy_buffering on;
    proxy_buffers 4 128k;
    proxy_busy_buffers_size 256k;
    proxy_max_temp_file_size 256k;
    
  • 相关阅读:
    SDN实验2:Mininet 实验——拓扑的命令脚本生成
    2020软工实践第一次作业
    POJ2942-Knights of the Round Table
    POJ1966 ZOJ2182<无向图点连通度 Isap版>
    POJ1523(求割点)
    POJ2391(最大流Isap+Floyd+二分)
    POJ1087 ZOJ1157(最大流Isap+map映射)
    POJ1459(最大流Isap)
    数字的字符串处理 (转)
    POJ2112 最大流(Isap+Floyd+二分)
  • 原文地址:https://www.cnblogs.com/Createsequence/p/13288639.html
Copyright © 2020-2023  润新知