前端基础知识- nginx配置入门简介nginx指令配置文件config.conf proxy _ passagenttry _ files root和alias之间的区别include
前言
前端开发经常需要将前端静态资源部署到服务上,在这种情况下,通常使用nginx进行配置
nginx配置详细信息: nginx中文文档
以下是几种常用的配置
nginx命令启动nginx: start nginx关闭nginx: nginx -s stop重新启动nginx: nginx -s reload配置文件config.conf events { worker _ connectent }http { include mime.types; efault _ type应用程序/octet-stream; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #打开压缩配置#gzip on; server { #监听端口listen 80; #服务器server_name localhost; 位置/{根html; index index.html; } }} proxy_pass代理在前端开发中通过在webpack.config.js的devServer.proxy上设置代理来调试接口
在服务器上,静态资源和服务器可能具有不同的源(协议://域名:端口),在这种情况下需要代理
服务器{ listen 80; server_name localhost; 位置/{根html; index index.html; } location/API/{ # 3358 localhost/API /代理到http://test.com:8081/api/#静态资源请求界面时,请在此处输入http://localhost//实际请求代理的proxy _ pass http://test.com :8081/API/} try _ files自始至终匹配重定向的参数,否则匹配重定向到最后的参数
服务器{ listen 80; server_name localhost; 位置/web {根/工作区; index test.html; try _ files : $ URL $ URL/test.html; } }根与alias的区别server { listen 80; server_name localhost; 位置/web {根/工作区; index test.html;http://hostlocal:80/web当请求搜索静态资源时,在服务器根位置(/workspace/web )中搜索资源。 默认值为test.html
服务器{ listen 80; server_name localhost; 位置/web/{ alias/workspace/static/index test.html; 当请求搜索http://hostlocal:80/web/静态资源时,在服务器alias(/workspace/static )中搜索资源。 默认值为test.html。 也就是说,alias取代了localhost的路径,而实际查找的路径是alias
如果需要使用导入的配置,include可以使用include导入文件,使其看起来很干净
事件{ worker _ connections 1024; }http { include mime.types; efault _ type应用程序/octet-stream; sendfile on; keepalive_timeout 65; 服务器{ listen 80; server_name localhost; 位置/{根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; 位置/web {根/工作区; index test.html; }