nginx中的root and alias命令的区别

Ubuntu关于Nginx的命令:

1、安装Nginx:

apt-get install nginx

2、查看Nginx运行状态:

systemctl status nginx

3、启动Nginx:

systemctl start nginx

4、停止Nginx:

systemctl stop nginx

5、重启Nginx:

temctl restart nginx

Nginx 的核心设置主要在 Nginx config 文件中进行配置,下面我们来看下配置中root和alias的区别。

Nginx root指令

root 指定文件根文件夹对应的/URL 路径,例如,如果你的 Root 指令是 /var/www/http://wljslmz.cn,那么当用户请求 /static/img/wljslmz.png 时,Nginx 将为他们提供/var/www/http://wljslmz.cn/static/img/wljslmz.png

换句话说,将 URL 路径附加到根位置来形成要提供的最终文件路径。

举个例子:

server {
    server_name https://www.wljslmz.cn;
    listen 443;
 
    index index.html;
    root /var/www/wljslmz.cn;
 
    location / {
        try_files $uri $uri/ =404;
    }
 
    location ^~ /img {
        root /var/www/static;
        try_files $uri $uri/ =404;
    }
}

如上配置,当用户访问https://www.wljslmz.cn/img/wljslmz.png时,会找到/var/www/static/img/wljslmz.png图片。

Nginx alias指令

alias 指令就是将 URL 重新映射到根位置以外的其他目录,它对于从不同目录提供静态文件很有用,例如,如果位置 /static/ 的别名是 /var/www/static/images,那么当用户请求 /img/wljslmz.png 时,Nginx 将在 /var/www/static/images 中查找该文件。

我们同样举个例子:

server {
    server_name https://www.wljslmz.cn;
    listen 443;
 
    index index.html;
    root /var/www/wljslmz.cn;
 
    location / {
        try_files $uri $uri/ =404;
    }
 
    location ^~ /img {
        alias /var/www/static/images/;
        try_files $uri $uri/ =404;
    }
}

如上配置,当用户访问https://www.wljslmz.cn/img/wljslmz.png时,会找到/var/www/static/images/wljslmz.png图片。

我们要注意:对于alias指定的文件夹,官方虽然没有强制要求加“/”,但是我们最好加上,以便阅读。

root 和 alias 区别

  • root读取的时根目录。可以在server或location指令中使用。
  • alias只能在location指令中使用。

两者何时用?

  • 如果位置与别名路径的末尾匹配,最好使用root。
  • 如果从与 root 指定的目录不同的位置读取数据时,最好使用alias。

总结

Nginx在Web开发中出场率非常高,本文主要讲解了什么时Nginx,重点对比了Nginx配置中root和alias指令的用法和区别,希望本文对您有所帮助,有任何疑问,欢迎在下方评论区与我讨论!

用于一个代理配置的实例 

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        #root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
		
		
		
		
		location /mes {
	        alias   /data/mesweb/dist; #前端的包所在路径
			try_files $uri $uri/ /mes/index.html;         #按此顺序查找请求的文件
            index  index.html index.htm;
        }
		
 
		
		location /mesapp {
			alias   /data/mesapp/h5; #前端的包所在路径 此处不能使用root 关键字  root   /data/mesapp/h5
			try_files $uri $uri/ /mesapp/index.html;  #/mesapp/index.html  这里的路径名字也不能少啊
            index  index.html index.htm;
        }
		
 
			
		
		location /prod-api/{
			proxy_set_header Host $http_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;
			
			
    
			#2023/11/30 发布app 增加的配置
			proxy_set_header X-Forwarded-Proto $scheme;
			proxy_set_header X-NginX-Proxy true;
			
			proxy_pass http://localhost:9901/; #转发到后端
		}
		
		location /ureport/{
			proxy_set_header Host $http_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;
			
			
    
			#2023/11/30 发布app 增加的配置
			proxy_set_header X-Forwarded-Proto $scheme;
			proxy_set_header X-NginX-Proxy true;
			
			proxy_pass http://localhost:9901/ureport/; #转发到后端
		}
		
		
		location /report/{
			proxy_set_header Host $http_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;
			
			
    
			#2023/11/30 发布app 增加的配置
			proxy_set_header X-Forwarded-Proto $scheme;
			proxy_set_header X-NginX-Proxy true;
			
			proxy_pass http://localhost:9901/; #转发到后端
		}
		
		
		location /mes/ureport/{
			proxy_set_header Host $http_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;
			
			
    
			#2023/11/30 发布app 增加的配置
			proxy_set_header X-Forwarded-Proto $scheme;
			proxy_set_header X-NginX-Proxy true;
			
			proxy_pass http://localhost:9901/ureport/; #转发到后端
		}
		
		
		
		
		#进销存网址
		location /jxc {
			proxy_set_header Host $http_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;
			proxy_pass http://127.0.0.1:8001/jxc; #转发到后端
		}
		
		location /windpower{
			proxy_set_header Host $http_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;
			proxy_pass http://127.0.0.1:9001/; #转发到后端
		}
		
		
		
		
		
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Nginxrootalias是两个用于定义服务器文件路径的指令,它们的作用略有不同。 root指令用于定义服务器文件系统根目录的路径,例如: ``` server { listen 80; server_name example.com; root /var/www/example.com; ... } ``` 在上面的例子root指令定义了服务器上example.com域名的根目录是/var/www/example.com。如果用户请求example.com/abc.html,Nginx会在文件系统寻找/var/www/example.com/abc.html文件来返回给用户。 而alias指令则用于将一个URL路径映射到服务器文件系统的某个具体文件或目录,例如: ``` location /images/ { alias /var/www/images/; ... } ``` 在上面的例子alias指令定义了当用户请求/images/路径时,Nginx会将该请求映射到服务器文件系统的/var/www/images/目录下,并返回该目录对应的文件或子目录。 因此,rootalias区别在于它们分别用于定义不同的文件路径映射方式,root指令定义了服务器的根目录路径,而alias指令则可以将URL路径映射到任意文件或目录。 ### 回答2: 在Nginxrootalias都是用于指定服务器文件目录的指令,但它们有着不同的作用及使用场景。 首先,root指令是用于指定服务器的根目录。当客户端请求的URL路径与root配置指令相匹配时,Nginx服务器将会在root目录下寻找对应的文件。例如,当root /usr/share/nginx/html;时,访问http://example.com/index.html将会去到/usr/share/nginx/html/index.html。 不过,当URL路径带有一些前缀时,root指令就显得有些不够灵活了。例如,当访问http://example.com/blog/index.html时,此时root /usr/share/nginx/html;就无法确切地定位到实际的index.html文件,因为它只能去/usr/share/nginx/html/目录下寻找文件,而无法解释"/blog"前缀。这时候,就需要使用到alias指令。 alias指令是用于指定服务器文件目录及URL路径间的映射关系,能够将URL路径特定的部分映射到其他位置,同时保留其他部分的路径。例如,当alias /data/nginx/;时,访问http://example.com/files/picture.jpg将会被映射到/data/nginx/files/picture.jpg。 相比而言,虽然root指令简单易用,但它无法支持URL路径的重写,因此在实际的开发alias指令可能更为常用。而使用时,则需在两个指令之间进行明确的区分和合理配置。 ### 回答3: Nginx是一款常用的Web服务器,它有两个重要的配置指令:rootalias。这两个指令的作用是为请求的URL(统一资源定位符)指定相应的文件路径。下面我将详细解释这两个指令的区别root指令: root指令指定了请求URL的根路径,它会将请求与根目录进行匹配,并找到对应的文件来响应请求。root指令是与location块一起使用的,其的location块指定了请求URL的匹配规则。 例如,如果我们在Nginx配置文件使用以下指令: location /images/ { root /var/www; } 那么当用户请求URL为http://example.com/images/logo.png 时,Nginx会在/var/www/images/下查找logo.png文件,并将其返回给用户。 alias指令: alias指令与root指令有些类似,也是将请求URL与文件路径进行匹配。不同之处在于,alias不会将请求URL的部分路径作为文件路径的一部分,而是将请求URL的部分路径替换为指定的文件路径。 例如,我们在Nginx配置文件使用以下指令: location /images/ { alias /var/www/images/; } 当用户请求URL为http://example.com/images/logo.png 时,Nginx会在/var/www/images/下查找logo.png文件,并将其返回给用户。这里的区别在于,如果我们使用root指令,Nginx会在/var/www/images/images/下查找logo.png文件。 总结: root指令和alias指令都是与location块一起使用,用于将请求URL与文件路径进行匹配。root指令将请求URL的路径与根目录进行匹配,而alias指令将请求URL的路径部分替换为指定的文件路径。理解这两个指令的区别非常重要,可以根据实际需求合理地配置Nginx,提高Web服务器的性能和安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值