Skip to content

Instantly share code, notes, and snippets.

@eagleon
Created July 2, 2015 02:09
Show Gist options
  • Save eagleon/26027877a722d247fefa to your computer and use it in GitHub Desktop.
Save eagleon/26027877a722d247fefa to your computer and use it in GitHub Desktop.
nginx札记

nginx札记

##1. 为每个域名开启不同的调用配置文件,不同的目录 每个域名一个文件的写法,首先打开nginx域名配置文件存放目录:/usr/local/nginx/conf/servers ,如要绑定域名jiangren.us 则在此目录建一个文件:jiangren.us.conf 然后在此文件中写规则,如:

server 
{ 
    listen  80; 
    server_name jiangren.us;             #绑定域名 
    index index.htm index.html index.php;  #默认文件 
    root /home/www/jiangren.us;              #网站根目录
    include location.conf;                 #调用其他规则,也可去除
}

然后重起nginx服务器,域名就绑定成功了 nginx服务器重起命令:/etc/init.d/nginx restart

##2. 开启文件浏览功能,用于下载文件夹下的内容

server {   
        root /data/www/file #指定实际目录绝对路径;   
        autoindex on; #开启目录浏览功能;   
        autoindex_exact_size off;  #关闭详细文件大小统计,让文件大小显示MB,GB单位,默认为b
        autoindex_localtime on;  #开启以服务器本地时区显示文件修改日期!   
}

##3. 开启所有*.jiangren.us都转向到jiangren.us

server {
    listen 80;
    server_name www.jiangren.us jiangren.us;
    if ($host = 'www.jiangren.us' ) {
        rewrite ^/(.*)$ http://jiangren.us/$1 permanent;
    }
}

或者,所有的非顶级域名都转过来

if ($host != 'jiangren.us' ) {
    rewrite ^/(.*)$ http://jiangren.us/$1 permanent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment