前端基础 - nginx配置入门

前言

在前端开发过程中经常是需要把前端静态资源放到服务器中,这时经常用到nginx来配置
nginx配置详情:Nginx 中文文档
下面列出几个经常用到的配置

nginx指令

  1. 启动 nginx: start nginx
  2. 关闭 nginx: nginx -s stop
  3. 重启 nginx: nginx -s reload

配置文件 config.conf

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #打开压缩配置
    #gzip  on;

    server {
    	#监听的端口
        listen       80;
        # 服务器
        server_name  localhost;
        
        location / {
            root   html;
            index  index.html;
        }
    }
}

proxy_pass 代理

在前端开发中经常在webpack.config.js的devServer.proxy中设置代理,以便调试接口
在服务器上有可能会静态资源和服务器不同源(协议://域名:端口),这时候就需要用到代理

    server {
        listen       80;
        server_name  localhost;
        
        location / {
            root   html;
            index  index.html;
        }
        
        location /api/ {
        	# 把 http://localhost/api/ 代理到 http://test.com:8081/api/
        	# 静态资源请求接口的时候走这里 http://localhost/api/
        	#实际请求代理到这里 http://test.com:8081/api/
           proxy_pass http://test.com:8081/api/
        }
    }

try_files

会从第一个开始匹配,匹配到最后一个,如果匹配不到重定向到最后一个的参数

 server {
        listen       80;
        server_name  localhost;

        location /web {
            root   /workspace;
            index  test.html;
            try_files:$url $url/test.html;
        }
    }

root 和 alias的区别

 server {
        listen       80;
        server_name  localhost;

        location /web {
            root   /workspace;
            index  test.html;
        }
    }

在请求 http://hostlocal:80/web 查找静态资源的时候就会到服务器 root+localhost(/workspace/web)查找资源,默认是test.html

 server {
        listen       80;
        server_name  localhost;
        location /web/ {
            alias /workspace/static/;
            index  test.html;
        }

    }

在请求 http://hostlocal:80/web/ 查找静态资源的时候就会到服务器 alias (/workspace/static)查找资源,默认是test.html;也就是说alias会替代localhost的路径,实际查找的路径是alias

include

当需要用到导入的配置的时候可以使用include导入文件,看起来更加整洁

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;
        
        location / {
            root   html;
            index  index.html;
        }
    }
	include config/test1.conf
	# include config/*.conf
	...
}

和nginx.conf同一文件夹下的config文件夹下的test1.conf文件

# config/test1.conf
 server {
        listen       80;
        server_name  localhost;

        location /web {
            root   /workspace;
            index  test.html;
        }
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值