使用Nginx配置同一端口访问不同路径下的文件

使用Nginx配置同一端口访问不同路径下的文件

  1. 编辑配置文件
/etc/nginx/nginx.conf
# http  
  server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name www.asimok.com;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        root /root/mq_blog/public;
        index index.html;
        }

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

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
        return 301 https://$host$request_uri;

    }
#https
   server{
        #监听443端口
        listen 443 ssl;
        #对应的域名,把baofeidyz.com改成你们自己的域名就可以了
        server_name www.asimok.com;
        #从腾讯云获取到的第一个文件的全路径
        ssl_certificate /etc/ssl/1_www.asimok.site_bundle.crt;
        #从腾讯云获取到的第二个文件的全路径
        ssl_certificate_key /etc/ssl/2_www.asimok.site.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;
        #这是我的主页访问地址,因为使用的是静态的html网页,所以直接使用location就可以完成了。
        location / {
               #文件夹
               root /root/mq_blog/public;
               #主页文件
               index index.html;
        }
			# 大西瓜
        location /daxigua {
               alias /root/daxigua;
               index index.html;
        }
    }
  1. 保存退出
  2. 对配置文件进行校验

保存配置文件之后执行:

nginx -t

susccessful即可

image-20201209112907309

  1. 重启nginx服务
service nginx start
  1. 重新加载配置文件
nginx -s reload

多个路径配置

				location /daxigua {
               alias /root/daxigua;
               index index.html;
        }

使用root会将location后的daxigua追加在路径的尾部,在访问时就会访问到/root/daxigua/daxigua路径下去。
将root改成alias则不会将daxigua追加在路径尾部,访问时就为正确路径/root/daxigua。

测试

https://www.asimok.site/daxigua

https://www.asimok.site


版权声明:本文为qq_39827677原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。