CentOS下安装Nginx 自启动 + 跨域
准备工作
在安装nginx前,需要确保系统已经安装了g++、gcc、 ncurses-devel 、penssl-devel、pcre-devel和zlib-devel软件。
#yum install gcc gcc-c++ ncurses-devel
#yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel
这些如果系统有可安装没有的
检查系统安装的ningx
#find -name nginx
卸载系统原有的nginx
#yum remove nginx
编译安装nginx
#wget http://nginx.org/download/nginx-1.11.10.tar.gz //下载
#tar -zxf nginx-1.11.10.tar.gz //解压
#mv nginx-1.11.10 nginx //重命名
#cd nginx
#./configure --prefix=/opt/nginx //--prefix指定安装目录,不要安装在当前解压目录,否则会出错
#make
#make install
安装服务实现自启动
建立服务文件
#vim /lib/systemd/system/nginx.service
输入以下内容
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/opt/nginx/sbin/nginx
ExecReload=/opt/nginx/sbin/nginx -s reload
ExecStop=/opt/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
为服务文件设置权限
#chmod 754 /lib/systemd/system/nginx.service
设置开机自启动
#systemctl start nginx.service
#systemctl enable nginx.service
我的项目(由于装了apache , nginx端口不能监听80)
106.xx.xx.146:8020
/opt/nginx/html
安装包 /usr/src nginx
/opt/nginx/conf/nginx.conf
反向代理解决跨域
/usr/local/nginx/conf
https://www.cnblogs.com/cheyunhua/p/8110489.html
dev环境
E:\code\vueAdmin-template\config\index.js
module.exports = {
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
'/gp': {
target: 'http://106.xx.xx.xx:8090/', //设置调用接口域名和端口号别忘了加http
changeOrigin: true,
pathRewrite: {
'^/gp': '/' //这里理解成用‘/gp’代替target里面的地址,组件中调接口时直接用gp代替
// 比如我要调用'http://0.0:300/user/add',直接写‘/gp/user/add’即可
}
}
},
vueAdmin-template\config\dev.env.js
'use strict'
const merge = require('webpack-merge')
const prodEnv = require('./prod.env')
module.exports = merge(prodEnv, {
NODE_ENV: '"development"',
// BASE_API: '"https://easy-mock.com/mock/5950a2419adc231f356a6636/vue-admin"',
// BASE_API:
'"http://xx.xx.xx.xx:8090/wbapi/"'
BASE_API: '"/gp/wbapi/"' //index.js 中 proxyTable作了代理
})
build环境
module.exports = {
NODE_ENV: '"production"',
// BASE_API:
'"http://106.xx.xx.xx:8100/wbapi/"',
BASE_API: '"/proxy/html/wbapi/"',
}
在nginx.conf 中 , location / {}下面加上: 把/proxy/html/代理指向 http:// 106.xx.xx.xx:8100/
server {
listen 8020;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location ^~/proxy/html/ {
rewrite ^/proxy/html/(.*)$ /$1 break;
proxy_pass http://106.xx.xx.xx:8100/;
}
然后把build的目录文件,放入服务器中
root /usr/share/nginx/html; 指向的目录(或者默认目录 nginx/html)
http://106.xx.xx.212:8080/pound
http://106.xx.xx.146:8020/