ubuntu前端Nginx跨域

677 阅读1分钟

安装

  1. 安装
sudo apt-get update
// 下载安装nginx
sudo apt-get install nginx

2.测试安装

在浏览启输入localhost:

nginx基本操作

// 重启nginx
sudo service nginx restart
// 启动nginx
sudo service nginx start
// 停止nginx
sudo service nginx stop
// 查看进程
ps -ef|grep nginx

nginx配置文件

  1. conf.d:用户自己定义的conf配置文件
  2. sites-available:系统默认设置的配置文件
  3. sites-enabled:由sites-available中的配置文件转换生成
  4. nginx.conf:汇总以上三个配置文件的内容 ,同事配置我们所需要的参数

配置nginx

  1. 在conf.d文件下创建自己的配置文件。例:myconf.conf
  2. myconf.conf中添加
server {
    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://localhost:8080
    }

    # 所有跨域访问以 /api 开头
    location /api {
        rewrite ^/api/(.*)$ /$1 break;
        proxy_pass 你需要代理到的ip地址;
   }
}
  1. 重启nginx

请求

axios({
    method:'post',
    url:'http://localhost/api/xxxxxxxxx',
})