我后台的项目是用部署在tomcat,想要做一个前后端分离的项目,但是遇到了跨域请求的问题,在网上看到可以利用Nginx配置虚拟服务解决这一问题,但是在网上查了很多相关的配置,由于没有学过Nginx,所以没能配置成功。通过自学,终于搞清楚怎么弄了。下面就放出我成功的nginx.conf中一个虚拟服务的配置:
server {
listen 80;#这里是需要在浏览器中输入的端口号
server_name localhost;#这里配置浏览器中输入的网址,所以我的浏览器访问的地址是 localhost ,由于80端口是默认端口,所以不需要输入
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root E:\webStromWorkSpace\ssspProject;#这里配置我页面存放的根目录
index list.html index.html; #这里的list.html是项目根目录要访问的页面
}
location /ssspProject/ {#其中url中包含 ssspProject的会转到下面配置的地址
proxy_pass http://127.0.0.1:8080;#这是我发送异步请求的地址,这里的端口号后面不需要再加/,否则转发不成功
proxy_redirect default; #我发送ajax请求的接口名是 var url = "/ssspProject/employee/emps";
}
#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;
#}
# 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;
#}
}
由于我目前没有掌握Nginx的使用,所以只能写这么多,只希望和我同样情况的小伙伴能够在我这里有所启发。