最近在搞一个项目,初次接触时头都大了,因为不了解远程代理之类的知识,vue中的第一意识就是$npm run dev后,直接访问配置的端口号就ok了,当然远程数据直接mock到本地~ 都是泪,虽然运行出来了,但是mock的数据是基于原来线上的项目F12调试模式下,看都调用了哪些接口地址,记录下来充当mock的数据。直到遇到了Nginx,根据自己的项目配置了一上午,终于不用本地mock数据了,如何利用Nginx将vue 打包后的项目看到效果?
一.初识nginx
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等
二.nginx下载
官方网址:http://nginx.org/en/download.html
下载完成以后,得到nginx压缩包
三、nginx启动
注意不要直接双击nginx.exe,这样会导致很多问题。可以使用命令行工具进行nginx的启动、停止和重启工作。使用Win+R快捷键打开命令行窗口,并切换到nginx.exe所在的目录。
$ start nginx 启动nginx
$ nginx -s stop 来快速停止nginx
$ nginx -s quit 完整的停止nginx。
四、运行预览
在执行cnpm run build命令之前,不需要改任何东西,就和cnpm run dev 一样的单纯
难道不需要改配置吗?不需要!!!!但是我们还是来看看Nginx的conf目录下的nginx.conf文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#前面是默认的 http { //.省略n./ server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://www.baidu.com/; } } 配置本地nginx,此时可访问localhost 自动跳转百度。 下面是配置https,大致结构和上述差不多。但是配置很恶心,小心应付,一点点测试。 # 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; # } #} } |
下面是根据自己的项目配置的:很长,如果没有什么问题的话,可以忽略了。
hosts中配置: 127.0.0.1 qaminiapp.qiakr.flyco.net.cn
.conf中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
...... http { ...... server { listen 80; server_name qaminiapp.qiakr.flyco.net.cn; location ~* \.json$ { proxy_pass http://192.168.20.125:8080; } //将下面合并到上面 可以访问qaminiapp.qiakr.flyco.net.cn/.../../xxx.json #location /rest/ { # proxy_pass http://192.168.20.125:8080; #} #location /admin/ { # proxy_pass https://192.168.20.125:8080; #} #add begin location /admin5/ { root F:/software/nginx-1.10.1/xmall_admin/dist/modules; index login.html index.html index.htm; } location ~/static/(.*)\.(css|js)$ { root F:/software/nginx-1.10.1/xmall_admin/dist/; index index.html index.htm; } location ~/admin5/(.*)\.(htm|do)$ { proxy_pass http://192.168.20.125:8080; # proxy_set_header Host $host; //这里注释了,不然好像有问题 proxy_set_header X-Forward-For $remote_addr; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Origin' '192.168.20.125'; } #add over location /mall5/ { root F:/software/nginx-1.10.1/xmall_admin/dist/modules; index login.html index.html index.htm; #proxy_pass http://127.0.0.1:80; #proxy_set_header Host $http_host; #proxy_set_header X-Forward-For $remote_addr; #add_header 'Access-Control-Allow-Credentials' 'true'; #add_header 'Access-Control-Allow-Origin' '127.0.0.1'; } location /mall/ { proxy_pass http://127.0.0.1:8080; } # location ~* /mall { # rewrite /mall/(.*) /$1 break; # rewrite /mall /$1 break; # proxy_pass http://127.0.0.1:8080; # } location ~/mallStatic/(.*)\.(css|js)$ { root F:/software/nginx-1.10.1/xmall_admin/dist/; index index.html index.htm; #proxy_pass http://127.0.0.1:80; #proxy_set_header Host $http_host; #proxy_set_header X-Forward-For $remote_addr; #add_header 'Access-Control-Allow-Credentials' 'true'; #add_header 'Access-Control-Allow-Origin' '127.0.0.1'; } location ~/mall5/(.*)\.(htm|do)$ { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $http_host; proxy_set_header X-Forward-For $remote_addr; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Origin' '127.0.0.1'; } rewrite ^(.*)\;jsessionid=(.*)$ $1 break; location / { root /data/www; index index.html index.htm index.php; } location ~ \.php$ { root html; fastcgi_pass 192.168.20.125:9000; fastcgi_index $uri/forum.php; fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name; include fastcgi_params; } location ^~ /file/ { root /data/www; index index.html index.htm; } location ^~ /product/ { root /data/www; index index.html index.htm; } location ^~ /ipa/ { root /data/www; index index.html index.htm; } location ^~ /apk/ { root /data/www; index index.html index.htm; } location ^~ /role/ { root /data/www; index index.html index.htm; } location ^~ /druid/ { proxy_pass http://127.0.0.1:8080; } location ^~ /css/ { proxy_pass http://127.0.0.1:8080; } location ^~ /js/ { proxy_pass http://127.0.0.1:8080; } location ^~ /analysis/ { proxy_pass http://192.168.20.125:19000/sonar/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ^~ /sonar { proxy_pass http://192.168.20.125:19000/sonar; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } location ^~ /easekf { root /data/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.(htm|css|js|jpg|png|swf|gif)$ { proxy_set_header Host $host; proxy_set_header X-real-ip $remote_addr; proxy_set_header X-Forward-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8080; if ($request_method = 'POST') { add_header 'Access-Control-Allow-Origin' 'http://115.29.220.239:5701'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; } } } server { listen 443; server_name qaminiapp.qiakr.flyco.net.cn; ssl on; ssl_certificate F:/software/nginx-1.10.1/conf/qamall.pem; ssl_certificate_key F:/software/nginx-1.10.1/conf/qamall.key; location ~* \.json$ { proxy_pass http://192.168.20.125:8080; } #add location /admin5/ { root F:/software/nginx-1.10.1/xmall_admin/dist/modules; index login.html index.html index.htm; } location ~/static/(.*)\.(css|js)$ { root F:/software/nginx-1.10.1/xmall_admin/dist/; index index.html index.htm; } location ~/admin5/(.*)\.(htm|do)$ { proxy_pass http://192.168.20.125:8080; # proxy_set_header Host $host; proxy_set_header X-Forward-For $remote_addr; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Origin' '192.168.20.125'; } #add over location /mall5/ { root F:/software/nginx-1.10.1/xmall_admin/dist/modules; index login.html index.html index.htm; #proxy_pass http://127.0.0.1:80; #proxy_set_header Host $http_host; #proxy_set_header X-Forward-For $remote_addr; #add_header 'Access-Control-Allow-Credentials' 'true'; #add_header 'Access-Control-Allow-Origin' '127.0.0.1'; } location /mall/ { proxy_pass http://127.0.0.1:8080; } # location ~* /mall { # rewrite /mall/(.*) /$1 break; # rewrite /mall /$1 break; # proxy_pass http://127.0.0.1:8080; # } location ~/mallStatic/(.*)\.(css|js)$ { root F:/software/nginx-1.10.1/xmall_admin/dist/; index index.html index.htm; #proxy_pass http://127.0.0.1:80; #proxy_set_header Host $http_host; #proxy_set_header X-Forward-For $remote_addr; #add_header 'Access-Control-Allow-Credentials' 'true'; #add_header 'Access-Control-Allow-Origin' '127.0.0.1'; } location ~/mall5/(.*)\.(htm|do)$ { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $http_host; proxy_set_header X-Forward-For $remote_addr; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Origin' '127.0.0.1'; } rewrite ^(.*)\;jsessionid=(.*)$ $1 break; location / { root /data/www; index index.html index.htm index.php; } location ~ \.php$ { root html; fastcgi_pass 192.168.20.125:9000; fastcgi_index $uri/forum.php; fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name; include fastcgi_params; } location ^~ /file/ { root /data/www; index index.html index.htm; } location ^~ /product/ { root /data/www; index index.html index.htm; } location ^~ /ipa/ { root /data/www; index index.html index.htm; } location ^~ /apk/ { root /data/www; index index.html index.htm; } location ^~ /role/ { root /data/www; index index.html index.htm; } location ^~ /druid/ { proxy_pass http://127.0.0.1:8080; } location ^~ /css/ { proxy_pass http://127.0.0.1:8080; } location ^~ /js/ { proxy_pass http://127.0.0.1:8080; } location ^~ /analysis/ { proxy_pass http://192.168.20.125:19000/sonar/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ^~ /sonar { proxy_pass http://192.168.20.125:19000/sonar; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } location ^~ /easekf { root /data/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.(htm|css|js|jpg|png|swf|gif)$ { proxy_set_header Host $host; proxy_set_header X-real-ip $remote_addr; proxy_set_header X-Forward-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8080; if ($request_method = 'POST') { add_header 'Access-Control-Allow-Origin' 'http://115.29.220.239:5701'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; } } } } |