前言
作为前端开发人员,通常只是写写业务代码,资源引用。至于部署到服务器上,使其运行起来基本上是运维人员在处理。但是实际项目中,往往需要我们也要掌握部署的相关的知识。例如nginx,端口,https怎么配置,跨域了如何解决等等。
前端部署的方案有哪些
- Nginx
- 容器部署
轻量强大的Nginx
nginx通常作为静态资源服务器和网关使用,用户转发http请求。
前端常用的特性
- 静态服务器
- 域名绑定
- 端口设置
- 跨域设置
- 代理转发
- 缓存设置
- https
- .....等
Nginx安装
brew install nginx
没有brew的参考Homebrew安装
执行nginx -version,正常显示版本就安装完成了。
Nginx的配置
在配置Nginx之前,我们先明确目标,需要做什么?
通常来说,我们准备一个web服务的需要基本具备以下特性:
- 能够通过域名特定端口访问
- 配置http和https
- 资源缓存
#user nobody;
# 指定工作衍生进程数
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8080;
#绑定的服务名
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}
location ~* ^.+.(jpg|jpeg|gif)$ {
root /xxx/www;
access_log off;
expires 30d;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ .php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include servers/*;
上面是nginx初始化时候的配置,包含http,https和server_name的绑定。前端经常遇到的跨域问题,nginx也能很方便的解决。
第一种解决方式是反向代理:
server
{
listen 3003;
server_name localhost;
## = /表示精确匹配路径为/的url,真实访问为http://localhost:5500
location = / {
proxy_pass http://localhost:5500;
}
## /no 表示以/no开头的url,包括/no1,no/son,或者no/son/grandson
## 真实访问为http://localhost:5500/no开头的url
## 若 proxy_pass最后为/ 如http://localhost:3000/;匹配/no/son,则真实匹配为http://localhost:3000/son
location /no {
proxy_pass http://localhost:3000;
}
## /ok/表示精确匹配以ok开头的url,/ok2是匹配不到的,/ok/son则可以
location /ok/ {
proxy_pass http://localhost:3000;
}
}
第二种解决方式是添加头部:
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,Content-Type,Cache-Control,User-Agent,Keep-Alive,Authorization,Accept,X-Mx-ReqToken,Origin,X-Requested-With,X-CustomHeader,If-Modified-Since,Cache-Control,If-Modified-Since';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' 'http://source01.odocker.com';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE';
return 204;
}
配置完毕后nginx reload重新载入就ok了。
到这里基本上前端完成一个基础前端的部署基本完成了,nginx还具备非常强大的功能,比如负载均衡,作为网关来控制流量等等。更多功能请查看官网nginx
