配置location块

  • location [ = | ~ | ~* | ^~ ] url {...}
  • 详解请见:http://lightless.me/archives/nginx-location-conf.html

配置请求根目录

  • root path
location /data/
{
    root /locationtest1;
}

当location块接收到/data/index.html请求的时候,将在/locationtest1/data/目录下寻找index.html并进行响应。

更改location的URI

  • alias path;

基于密码配置Nginx的访问权限

  • auth_basic string | off;
  • auth_basic_user_file file;
  • 密码文件格式如下:
name1:password1
name2:password2:comment
name3:password3

加密密码可以使用crypt()函数进行密码加密的格式,也可以使用下面的命令来生成:

htpasswd -c -d /nginx/conf/pass_file username

一个配置文件的例子

user www www;
worker_process 3;
error_log logs/error.log;

pid nginx.pid;

events{
    
    use epoll;

    worker_connections 1024;

}

http {
    
    include mime.types;
    default_type application/octet-stream;

    sendfile on;

    keepalive_timeout 60;

    log_format access.log
    '$remote_addr - [$time_local] - "$request" - "$http_user_agent"';

    server {
        listen 8081;
        server_name myServer1;
        access_log /myweb/server1/log/access.log;

        error_page 404 /404.html
    
        location /server1/location1 {
            root /myweb;
            index index.svr1-loc1.html;
        }

        location /server1/location2 {
            root /myweb;
            index index.svr1-loc2.html;
        }
    }

    server {
        listen 8082;
        server_name 192.168.1.3;
        access_log /myweb/server2/log/access.log;
        error_page 404 /404.html
        
        location /server2/location1 {
            root /myweb;
            index index.srv2-loc1.html;
        }

        location /server2/location2 {
            root /myweb;
            index index.serv2-loc2.html;
        }

        location = /404.html {
            root /myweb;
            index 404.html;
        }
    }
}