当前位置:   article > 正文

nginx中的root and alias命令的区别_nginx root

nginx root

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 路径附加到根位置来形成要提供的最终文件路径。

举个例子:

  1. server {
  2. server_name https://www.wljslmz.cn;
  3. listen 443;
  4. index index.html;
  5. root /var/www/wljslmz.cn;
  6. location / {
  7. try_files $uri $uri/ =404;
  8. }
  9. location ^~ /img {
  10. root /var/www/static;
  11. try_files $uri $uri/ =404;
  12. }
  13. }

如上配置,当用户访问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 中查找该文件。

我们同样举个例子:

  1. server {
  2. server_name https://www.wljslmz.cn;
  3. listen 443;
  4. index index.html;
  5. root /var/www/wljslmz.cn;
  6. location / {
  7. try_files $uri $uri/ =404;
  8. }
  9. location ^~ /img {
  10. alias /var/www/static/images/;
  11. try_files $uri $uri/ =404;
  12. }
  13. }

如上配置,当用户访问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指令的用法和区别,希望本文对您有所帮助,有任何疑问,欢迎在下方评论区与我讨论!

用于一个代理配置的实例 

  1. # For more information on configuration, see:
  2. # * Official English Documentation: http://nginx.org/en/docs/
  3. # * Official Russian Documentation: http://nginx.org/ru/docs/
  4. user nginx;
  5. worker_processes auto;
  6. error_log /var/log/nginx/error.log;
  7. pid /run/nginx.pid;
  8. # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
  9. include /usr/share/nginx/modules/*.conf;
  10. events {
  11. worker_connections 1024;
  12. }
  13. http {
  14. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  15. '$status $body_bytes_sent "$http_referer" '
  16. '"$http_user_agent" "$http_x_forwarded_for"';
  17. access_log /var/log/nginx/access.log main;
  18. sendfile on;
  19. tcp_nopush on;
  20. tcp_nodelay on;
  21. keepalive_timeout 65;
  22. types_hash_max_size 4096;
  23. include /etc/nginx/mime.types;
  24. default_type application/octet-stream;
  25. # Load modular configuration files from the /etc/nginx/conf.d directory.
  26. # See http://nginx.org/en/docs/ngx_core_module.html#include
  27. # for more information.
  28. include /etc/nginx/conf.d/*.conf;
  29. server {
  30. listen 80;
  31. listen [::]:80;
  32. server_name _;
  33. #root /usr/share/nginx/html;
  34. # Load configuration files for the default server block.
  35. include /etc/nginx/default.d/*.conf;
  36. error_page 404 /404.html;
  37. location = /404.html {
  38. }
  39. error_page 500 502 503 504 /50x.html;
  40. location = /50x.html {
  41. }
  42. location /mes {
  43. alias /data/mesweb/dist; #前端的包所在路径
  44. try_files $uri $uri/ /mes/index.html; #按此顺序查找请求的文件
  45. index index.html index.htm;
  46. }
  47. location /mesapp {
  48. alias /data/mesapp/h5; #前端的包所在路径 此处不能使用root 关键字 root /data/mesapp/h5
  49. try_files $uri $uri/ /mesapp/index.html; #/mesapp/index.html 这里的路径名字也不能少啊
  50. index index.html index.htm;
  51. }
  52. location /prod-api/{
  53. proxy_set_header Host $http_host;
  54. proxy_set_header X-Real-IP $remote_addr;
  55. proxy_set_header REMOTE-HOST $remote_addr;
  56. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  57. #2023/11/30 发布app 增加的配置
  58. proxy_set_header X-Forwarded-Proto $scheme;
  59. proxy_set_header X-NginX-Proxy true;
  60. proxy_pass http://localhost:9901/; #转发到后端
  61. }
  62. location /ureport/{
  63. proxy_set_header Host $http_host;
  64. proxy_set_header X-Real-IP $remote_addr;
  65. proxy_set_header REMOTE-HOST $remote_addr;
  66. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  67. #2023/11/30 发布app 增加的配置
  68. proxy_set_header X-Forwarded-Proto $scheme;
  69. proxy_set_header X-NginX-Proxy true;
  70. proxy_pass http://localhost:9901/ureport/; #转发到后端
  71. }
  72. location /report/{
  73. proxy_set_header Host $http_host;
  74. proxy_set_header X-Real-IP $remote_addr;
  75. proxy_set_header REMOTE-HOST $remote_addr;
  76. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  77. #2023/11/30 发布app 增加的配置
  78. proxy_set_header X-Forwarded-Proto $scheme;
  79. proxy_set_header X-NginX-Proxy true;
  80. proxy_pass http://localhost:9901/; #转发到后端
  81. }
  82. location /mes/ureport/{
  83. proxy_set_header Host $http_host;
  84. proxy_set_header X-Real-IP $remote_addr;
  85. proxy_set_header REMOTE-HOST $remote_addr;
  86. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  87. #2023/11/30 发布app 增加的配置
  88. proxy_set_header X-Forwarded-Proto $scheme;
  89. proxy_set_header X-NginX-Proxy true;
  90. proxy_pass http://localhost:9901/ureport/; #转发到后端
  91. }
  92. #进销存网址
  93. location /jxc {
  94. proxy_set_header Host $http_host;
  95. proxy_set_header X-Real-IP $remote_addr;
  96. proxy_set_header REMOTE-HOST $remote_addr;
  97. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  98. proxy_pass http://127.0.0.1:8001/jxc; #转发到后端
  99. }
  100. location /windpower{
  101. proxy_set_header Host $http_host;
  102. proxy_set_header X-Real-IP $remote_addr;
  103. proxy_set_header REMOTE-HOST $remote_addr;
  104. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  105. proxy_pass http://127.0.0.1:9001/; #转发到后端
  106. }
  107. }
  108. # Settings for a TLS enabled server.
  109. #
  110. # server {
  111. # listen 443 ssl http2;
  112. # listen [::]:443 ssl http2;
  113. # server_name _;
  114. # root /usr/share/nginx/html;
  115. #
  116. # ssl_certificate "/etc/pki/nginx/server.crt";
  117. # ssl_certificate_key "/etc/pki/nginx/private/server.key";
  118. # ssl_session_cache shared:SSL:1m;
  119. # ssl_session_timeout 10m;
  120. # ssl_ciphers HIGH:!aNULL:!MD5;
  121. # ssl_prefer_server_ciphers on;
  122. #
  123. # # Load configuration files for the default server block.
  124. # include /etc/nginx/default.d/*.conf;
  125. #
  126. # error_page 404 /404.html;
  127. # location = /40x.html {
  128. # }
  129. #
  130. # error_page 500 502 503 504 /50x.html;
  131. # location = /50x.html {
  132. # }
  133. # }
  134. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/697470
推荐阅读
相关标签
  

闽ICP备14008679号