Nginx基础之常用配置

Nginx 设置

Nginx的fastcgi模块参数设置

  • Nginx 有两个配置文件fastcgi_paramsfastcgi.conf,两者唯一的区别是,fastcgi.conf 多一个参数 SCRIPT_FILENAME,diff显示如下:

    $diff fastcgi fastcgi_params
    
    < fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    
  • vim 进入/usr/local/nginx/conf/fastcgi_params文件

    #请求的参数;如?app=123fastcgi_param 
    fastcgi_param  QUERY_STRING       $query_string;   
    
    ##请求的动作(GET,POST)
    fastcgi_param  REQUEST_METHOD     $request_method;
    
     #请求头中的Content-Type字段
    fastcgi_param  CONTENT_TYPE       $content_type;
    
     #请求头中的Content-length字段
    fastcgi_param  CONTENT_LENGTH     $content_length;
    
    #脚本名称
    fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
    
    #请求的地址不带参数
    fastcgi_param  REQUEST_URI        $request_uri;
    
    #与$uri相同
    fastcgi_param  DOCUMENT_URI       $document_uri;
    
    #网站的根目录。在server配置中root指令中指定的值
    fastcgi_param  DOCUMENT_ROOT      $document_root;
    
    #请求使用的协议,通常是HTTP/1.0或HTTP/1.1
    fastcgi_param  SERVER_PROTOCOL    $server_protocol;
    
    #https 如果value非空才进行设置
    fastcgi_param  HTTPS              $https if_not_empty;
    
    #cgi 版本
    fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
    
    #nginx 版本号,可修改、隐藏
    fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
    
    #客户端IP
    fastcgi_param  REMOTE_ADDR        $remote_addr;
    
    #客户端端口
    fastcgi_param  REMOTE_PORT        $remote_port;
    
    #服务器IP地址
    fastcgi_param  SERVER_ADDR        $server_addr;
    
    #服务器端口
    fastcgi_param  SERVER_PORT        $server_port;
    
    #服务器名,域名在server配置中指定的server_name
    fastcgi_param  SERVER_NAME        $server_name;
    
    可自定义变量
    fastcgi_param PATH_INFO $path_info;
    
    #在尾部另起一行追加即可保存跟fastcgi.conf 一致
    fastcgi_param  REDIRECT_STATUS    200;
    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    

    附:

    在php可打印出上面的服务环境变量
    如:echo $_SERVER['REMOTE_ADDR']


Nginx的常用指令解释

  • fastcgi_pass

    这个命令是指定将http代理到哪个fastcgi服务端接口。fastcgi_pass后面是填写fastcgi服务端地址的,这个地址可以是域地址,也可以是Uninx-域套接字, 另外也可以是upstream中设置的反向代理。

    fastcgi_pass localhost:9000;  #默认PHP起在9000端口
    fastcgi_pass unix:/tmp/fastcgi.socket;
    fastcgi_pass upstream_php5;  #这里指定的反向代理可以在nginx.conf中upstream中设置
    
  • fastcgi_param

    这个命令是设置fastcgi请求中的参数,默认参数在上面提到的fastcgi模块参数文件中,具体设置的东西可以在$_SERVER中获取到。
    比如你想要设置当前的机器环境,可以使用fastcgi_param ENV test;来设置。
    对于php来说,最少需要设置的变量有:

    fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;
    fastcgi_param QUERY_STRING    $query_string;
    
  • fastcgi_index

    这个命令设置了fastcgi默认使用的脚本。就是当SCRIPT_FILENAME没有命中脚本的时候,使用的就是fastcgi_index设置的脚本。

    fastcgi_index  index.php;
    
    
  • 示例

    以上三个命令能组成最基本的fastcgi设置了:

    location / {
      fastcgi_pass   localhost:9000;
      fastcgi_index  index.php;
     
      #下面这一个可以直接在fastcgi_param配置文件中指定
      fastcgi_param  SCRIPT_FILENAME  /home/www/scripts/php$fastcgi_script_name;
      fastcgi_param  QUERY_STRING     $query_string;
      fastcgi_param  REQUEST_METHOD   $request_method;
      fastcgi_param  CONTENT_TYPE     $content_type;
      fastcgi_param  CONTENT_LENGTH   $content_length;
    }
    

配置

  • 图片(或者静态文件)服务器配置

    server {
        listen       80;
        server_name  images.xxx.com img.movie.xxx.com;
        root /data/vhosts/xxx.com/images/public_html/;
        index index.shtml index.html index.htm;
        
        #如果是js、css、json文件可以指定压缩来减少传输文件大小
        gzip_types  text/plain application/x-javascript text/css  application/xml text/xml application/json;  
    }
    
  • 基础服务器

    server {
        listen      80;
        server_name  www.xxx.com;
        root /data/vhosts/xxxx.com/public_html/;
        index index.htm  index.php index.html index.shtml;
    
        location / {
          ssi on;
          ssi_silent_errors on;
          ssi_types  text/shtml;
          include other.conf;  #这里可以配置其他的公共配置,或者重写规则
        }
        location ~\.php$ {
          expires off;
          include fastcgi_params;        #fastcgi 指定的参数配置
          fastcgi_pass 127.0.0.1:9000;   #这里同上也可指定代理或socket
          fastcgi_index index.php;
          fastcgi_connect_timeout 300;
          fastcgi_send_timeout 300;
          fastcgi_read_timeout 300;
        }
       
        #配置ssi_inclue访问的目录不存在是指定的目录
        location ~ /ssi_include/ {
          if (!-e $request_filename) {
             rewrite ^(.*)$ /blank.html last;
          }
        }
       
        #配置静态文件默认的错误页面
        location ~(\.html|\.htm|\.shtml)$ {
          error_page   404 500 502 503 504  /404.html;
        }        
    }
    
    

Auth权限设置

  • step 1. 在根域名下面需要配置权限的目录设置location

    location /phpMyAdmin/ {
             allow   192.168.0.1;
             allow   xx.xx.xxx.xxx;
             allow   xx.xx.xxx.xxx;
             deny    all;
             auth_basic            "Restricted";
             auth_basic_user_file  /usr/local/nginx/conf/auth_phpmyadmin.pass;
     }
    
  • step2. 在 auth_basic_user_file 指定的文件下面增加账号密码,一行一个

    username1:password1
    username2:password2
    username3:password3
    username4:password4
    

反向代理设置

  • Nginx反向代理

    • 第一种反向代理:

       location / {
              proxy_pass    http://192.168.1.4:8099/;
              #若针对不同的目录进行代理把下面的配置放到根目录代理的上面
              #proxy_pass    http://192.168.1.4:8099/linuxtone/;
              proxy_redirect default ;
          }
      
    • 第二种反向代理:

      • upstream配置
      upstream xx.xxx.com {
          server 192.168.1.4:8099;
      }
      
      • 站点配置文件
      server
      {
          listen             80;
          server_name    bbs.linuxtone.conf;
          index index.html index.htm;
          root /date/vhosts/xxx.com/;
      
          location ~ ^/NginxStatus/ {
                  stub_status on;
                  access_log off;
           }
      
          location / {
               proxy_redirect off ;
               proxy_set_header Host $host;
               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header REMOTE-HOST $remote_addr;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
               client_max_body_size 50m; #缓冲区代理缓冲用户端请求的最大字节数,可以理解为保存到本地再传给用户
               client_body_buffer_size 256k;
               proxy_connect_timeout 30; #nginx跟后端服务器连接超时时间(代理连接超时)
               proxy_send_timeout 30;
               proxy_read_timeout 60; #连接成功后,后端服务器响应时间(代理接收超时)
               proxy_buffer_size 256k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 
               proxy_buffers 4 256k; #proxy_buffers缓冲区,网页平均在256k以下的话,这样设置
               proxy_busy_buffers_size 256k;
               proxy_temp_file_write_size 256k;
               proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
               proxy_max_temp_file_size 128m;
               proxy_ignore_client_abort on; #不允许代理端主动关闭连接
               #http://xx.xxx.com 指上面upstream块的名称
               proxy_pass   http://xx.xxx.com;
          }
      
  • 附Apache反向代理:

    #设置该域名转发给8080端口
    <VirtualHost *:80>
    	ServerAdmin webmaster@dummy-host2.example.com
    	ServerName www.xxx.com
    	ProxyRequests off
    	<Proxy *>
    		Order deny,allow
    		Allow from all
    	</Proxy>
    	ProxyPass / http://www.xxx.com:8080/
    	ProxyPassReverse / http://www.xxx.com:8080/
    </VirtualHost>
    
    • ProxyPassReverse一般和ProxyPass指令配合使用,此指令使Apache调整HTTP重定向应答中Location, Content-Location, URI头里的URL,这样可以避免在Apache作为反向代理使用时,。后端服务器的HTTP重定向造成的绕过反向代理的问题

禁止蜘蛛访问

  • 具体配置如下:

    #判断UA,如果UA不包含spider或者bot(不区分大小写),表示UA为正常用户
    #设置变量is_human值为yes
    if ($http_user_agent !~* "spider|bot") {
        set $is_human 'yes';
    }
    
    #当有任意请求的时候,该UA不是正常用户,则表示应该是蜘蛛类程序,则返回403
    location / {
        if ($is_human != 'yes') {
            return 403;
        }
    }
    
    

    或者:

    #当有任意请求的时候
    location / {
            #当访问者UA包含有spider或则bot的时候(不区分大小写),说明是蜘蛛类来访
        if ($http_user_agent ~* "spider|bot") {
            # 直接就屏蔽蜘蛛的整站访问
            return 403;
        }
    }
    
  • 给系统添加了robots.txt文件:

    User-agent: *
    Disallow: /
    
posted @ 2017-09-28 17:16  oneVillager  阅读(194)  评论(0编辑  收藏  举报
打赏

喜欢请打赏

扫描二维码打赏