Skip to content

nginx配置

nginx 配置单机器多域名

1.conf.d/default.conf 默认域名,若没命中,进入此处

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html/default;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

1.conf.d/www.my.com.conf www.my.com 和 my.com 进入会命中

server {
    listen       80;
    listen  [::]:80;
    server_name  www.my.com my.com;

    location / {
        root   /usr/share/nginx/html/my;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

常用功能

文件服务器

server {
    listen 80;  # 监听 HTTP 请求的端口,可以根据需要更改
    server_name files.localhost;  # 你的服务器名称或 IP 地址

    location / {
        alias /files/;  # 指定文件存储目录
        autoindex on;  # 启用自动列出目录内容
        autoindex_exact_size off;  # 显示文件大小的近似值
        charset utf-8;
    }
}

basic auth

  • htpasswd文件生成
printf "test:$(openssl passwd -crypt 123456)\n" >> /usr/local/nginx-1.5.1/conf/basicauth
  • 使用
server{
    listen 80;
    server_name  auth.localhost;

    location / {
        root   html;
        auth_basic "Please enter your username and password";
        auth_basic_user_file ../conf/basicauth; 
        index  index.html index.htm;
    }

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

Comments