大网数据广告活动
服务器运维问答

How to alias directories in nginx?

· 52CE 我爱测

I for the life of me cannot figure out how to make aliases for locations. Basically, I have /var/www on my server, which could contain any number of folders (for different applications). One of those is ViMbAdmin, located in /var…

问题原文

原帖链接 · ffxsam · CC BY-SA 3.0

I for the life of me cannot figure out how to make aliases for locations.

Basically, I have /var/www on my server, which could contain any number of folders (for different applications). One of those is ViMbAdmin, located in /var/www/vimbadmin, and the actual web files that should be served to the client are in /var/www/vimbadmin/public. Here's what I have so far, which is failing:

server {
    listen      80;
    server_name myserver.com;
    root        /var/www;

    index index.php;

    # Logs
    access_log  /var/log/nginx/vimbadmin.access.log;
    error_log   /var/log/nginx/vimbadmin.error.log;

    location /vimbadmin/public {
        try_files $uri $uri/ /index.php?$args;
    }

    location /mail2admin {
      alias /vimbadmin/public;
    }

    # Pass the PHP scripts to FastCGI server
    location ~ \.php$ {
        # Prevent Zero-day exploit
        try_files $uri =404;

        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        fastcgi_pass    unix:/var/run/php5-fpm.sock;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include         fastcgi_params;
    }
}

My goal is to set it up so that hackers can't just try to go to http://myserver.com/vimbadmin, so the actual URL will be http://myserver.com/mail2admin. What am I doing wrong? I'm really confused about the relationships between root, location, and alias.

社区高分解答

原帖链接 · Carcer · CC BY-SA 4.0

The location directive matches against the uri in the request. The root directive and alias directives are both used to indicate where in the filesystem to serve resources from, the difference being that when using root, the entire URI is still appended to the root; whereas when using alias, the location part is dropped. See the answer to https://stackoverflow.com/questions/10631933/nginx-static-file-serving-confusion-with-root-alias

So in your case, what you want probably looks like:

server {

    listen      80;
    server_name myserver.com;
    root        /var/www;

    index index.php;

    ...

    location /vimbadmin/ {
       return 404;
    }

    location /mail2admin/ {
      alias /var/www/vimbadmin/public;
      try_files $uri $uri/ /index.php?$args;
    }

    ...
}

This prevents anyone from going to myserver.com/vimbadmin/, but if they go to myserver.com/mail2admin/, nginx maps to /var/www/vimbadmin/public (if you used root /var/www/vimbadmin/public, nginx would be trying to serve the files from /var/www/vimbadmin/public/mail2admin, which is not what you want).

Generally speaking however trivial url obfuscation like this should not be considered any kind of defence against hackers. If you want to keep this admin panel safe you should rely on authentication of some kind, and potentially lock it down so it only allows access from approved IPs if that's viable for you. The application itself might offer some user authentication, and you can always add basic http auth. SSL would also be recommended (self-signed if it's something you're only using yourself, or now easy to do for free with LetsEncrypt.org if you want it to be trusted by other people's browsers in the general case).

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-01-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。