nginx:前后端分离解决跨域问题
=============================
location/{
proxy_passhttp://dev.abc.com;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
proxy_set_headerHost$host:$server_port;
add_header'Access-Control-Allow-Origin''http://m3.abc.com';
add_header'Access-Control-Allow-Credentials''true';
add_header'Access-Control-Allow-Methods''GET,POST,OPTIONS';
}
//////////////////////////////////////////////////////////////////////////
前后端分离配置:
=============================
hosts
127.0.0.1dev.abc.com
=============================
/usr/local/etc/nginx/nginx.conf
#usernobody;
worker_processes1;
#error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;
#pidlogs/nginx.pid;
events{
worker_connections1024;
}
http{
includemime.types;
#default_typeapplication/octet-stream;
#log_formatmain'$remote_addr-$remote_user[$time_local]"$request"'
#'$status$body_bytes_sent"$http_referer"'
#'"$http_user_agent""$http_x_forwarded_for"';
#access_loglogs/access.logmain;
sendfileon;
#tcp_nopushon;
#keepalive_timeout0;
keepalive_timeout65;
#gzipon;
server{
listen8080;
server_namelocalhost;
#charsetkoi8-r;
#access_loglogs/host.access.logmain;
location/{
roothtml;
indexindex.htmlindex.htm;
}
#error_page404/404.html;
#redirectservererrorpagestothestaticpage/50x.html
#
error_page500502503504/50x.html;
location=/50x.html{
roothtml;
}
#proxythePHPscriptstoApachelisteningon127.0.0.1:80
#
#location~\.php${
#proxy_passhttp://127.0.0.1;
#}
#passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000
#
#location~\.php${
#roothtml;
#fastcgi_pass127.0.0.1:9000;
#fastcgi_indexindex.php;
#fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
#includefastcgi_params;
#}
#denyaccessto.htaccessfiles,ifApache'sdocumentroot
#concurswithnginx'sone
#
#location~/\.ht{
#denyall;
#}
}
#anothervirtualhostusingmixofIP-,name-,andport-basedconfiguration
#
#server{
#listen8000;
#listensomename:8080;
#server_namesomenamealiasanother.alias;
#location/{
#roothtml;
#indexindex.htmlindex.htm;
#}
#}
#HTTPSserver
#
#server{
#listen443ssl;
#server_namelocalhost;
#ssl_certificatecert.pem;
#ssl_certificate_keycert.key;
#ssl_session_cacheshared:SSL:1m;
#ssl_session_timeout5m;
#ssl_ciphersHIGH:!aNULL:!MD5;
#ssl_prefer_server_cipherson;
#location/{
#roothtml;
#indexindex.htmlindex.htm;
#}
#}
includeservers/*;
}
=================================
/usr/local/etc/nginx/servers/yyb.conf
upstreamcinema.admin{
server127.0.0.1:10081;
}
server{
listen80;
server_namedev.abc.com;
location/api{
proxy_passhttp://cinema.admin;
add_headerCache-Controlno-cache;
add_headerCache-Controlprivate;
}
location/admin{
rewrite^/api/(.*)$/$1break;
proxy_passhttp://cinema.admin;
add_headerCache-Controlno-cache;
add_headerCache-Controlprivate;
}
location/{
root/Users/samson/Work/movikr/movikr_workspace/cinema-admin-all/cinema-admin-h5/src;
indexindex.htmlindex.htm;
add_headerCache-Controlno-cache;
}
}
=====================================
api服务:127.0.0.1:10081
前端:http://dev.abc.com
浏览器访问http://dev.abc.com
访问http://dev.abc.com/api/load调用http://127.0.0.1:10081/load的服务接口