赞
踩
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的区别。
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
图片。
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指定的文件夹,官方虽然没有强制要求加“/”,但是我们最好加上,以便阅读。
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 {
- # }
- # }
-
- }
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。