如果不在机器上部署后端怎么进行配置
来源:12-4 nginx配置和域名解析
李行知
2017-08-22
想不在机器上进行后端的配置,直接请求happymmall进行数据的传递可以吗?
这样的话代码要改哪些?Nginx怎么配置呢?
具体是怎么去请求资源,以及接口,这些都有跨域问题
第二个,现在没有注册域名,怎么进行Nginx服务的控制呢?打算部署到线上然后直接给人机器ip就可以访问的那种,自己看到的是两种方案,一种是多个ip,还有一个是设置location匹配规则,这个应该怎么考虑
2回答
-
课程里配置的接口地址是http:localhost:8080,换成线上地址http:www.happymmall.com就可以用课程里的后端的
022018-04-24 -
李行知
提问者
2017-08-23
这里算是自问自答了,来解决如果我们只是想单纯的在我们的云服务器上部署一个前端项目的话应该怎么解决,这里没有部署我们的admin项目,也没注册域名,首先要修改我们的webpack的代码
publicPath: WEBPACK_ENV === "dev" ? "/dist/" : "/dist/", //请用happymmall的接口的时候用的
虽然有点蠢但是还是够用的~
然后我自己做的时候是在自己的机器上进行打包
npm run dist_win
然后将打包好的dist目录放到自己的项目下面,我用的是MobaXterm进行的连接,可以往服务上上传文件,但是要注意不能上传文件夹,所以要先进行打包,当然我觉得老师课程中的那个部署应该更好,但是我这里有点图方便了
/root/L/project/mmall
其他同学如果想要部署得话,可以把后面的代码中的目录改成自己的就好了,然后进行nginx的配置
下面是配置文件
不过可以不用看了,如果看了后面的还不能解决再来对照吧
#user nobody; user root; 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 /usr/local/nginx/logs/access.log combined; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location = / { root /root/L/project/mmall/dist/view; index index.html index.htm; # proxy_pass http://localhost:8080; } location ~ .*\.html { root /root/L/project/mmall/dist/view; index index.html index.htm; } location ~ .*\.css$ { root /root/L/project/mmall; } location ~ .*\.js$ { root /root/L/project/mmall; } location ~ .*\.(png|jpg|gif|svg)$ { root /root/L/project/mmall; } location ~* \.(eot|otf|ttf|woff|svg|woff2)$ { root /root/L/project/mmall; add_header Access-Control-Allow-Origin *; } location / { proxy_pass http://www.happymmall.com; } #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; #} } # 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; # } #} }
主要修改的地方是
在头部加上
user root
不加这个会有403错误
配置location
location = / { root /root/L/project/mmall/dist/view; index index.html index.htm; # proxy_pass http://localhost:8080; } location ~ .*\.html { root /root/L/project/mmall/dist/view; index index.html index.htm; } location ~ .*\.css$ { root /root/L/project/mmall; } location ~ .*\.js$ { root /root/L/project/mmall; } location ~ .*\.(png|jpg|gif|svg)$ { root /root/L/project/mmall; } location ~* \.(eot|otf|ttf|woff|svg|woff2)$ { root /root/L/project/mmall; add_header Access-Control-Allow-Origin *; } location / { proxy_pass http://www.happymmall.com; }
这样,我们的页面访问资源,包括css,js,这些的时候,都是访问到我们的/root/project/mmall目录下对应的文件夹和文件了,同学自己写的时候,就写成自己的目录就好了
然后检查配置文件,重启Nginx,再输入自己的云服务ip就好了,因为我这里也是暂时为了简单没有注册域名的原因
172019-01-18
相似问题