介绍:Nginx功能丰富,可作为HTTP服务器,也可作为反向代理服务器,邮件服务器。支持FastCGI、SSL、Virtual Host、URL Rewrite、Gzip等功能。并且支持很多第三方的模块扩展。
作用:分布式部署
首先下载nginx: http://nginx.org/en/download.html
配置:conf/nginx.conf 基本意思
#user nobody; 进程数 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块:配置影响nginx服务器或与用户的网络连接,可不修改 events { worker_connections 1024; } #可以多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置 http { #定义nginx能识别的网络资源媒体类型(如,文本、html、js、css、流媒体等,cat conf/mime.types) include mime.types; #定义默认的type,如果不定义改行,默认为text/plain. default_type application/octet-stream; #其中main为日志格式的名字,后面的为nginx的内部变量组成的一串字符串。 #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 logs/access.log main; #是否调用sendfile函数传输文件,默认为off,使用sendfile函数传输,可以减少user mode和kernel mode的切换,从而提升服务器性能。 对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。 sendfile on; #当tcp_nopush设置为on时,会调用tcp_cork方法进行数据传输。 使用该方法会产生这样的效果:当应用程序产生数据时,内核不会立马封装包,而是当数据量积累到一定量时才会封装,然后传输。这样有助于解决网络堵塞问题。 默认值为on。举例:快递员收快递、发快递,包裹累积到一定量才会发,节省运输成本。 #tcp_nopush on; #该参数有两个值,第一个值设置nginx服务器与客户端会话结束后仍旧保持连接的最长时间,单位是秒,默认为75s。 第二个值可以省略,它是针对客户端的浏览器来设置的,可以通过curl -I看到header信息中有一项Keep-Alive: timeout=60,如果不设置就没有这一项。 第二个数值设置后,浏览器就会根据这个数值决定何时主动关闭连接,Nginx服务器就不操心了。但有的浏览器并不认可该参数。 keepalive_timeout 65; #gzip on; 是否开启gzip压缩
#配置虚拟主机的相关参数,一个http中可以有多个server。 server { listen 80; #监听的端口 server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; #文件根目录 index index.html index.htm; #首页的索引文件 } #error_page 404 /404.html;
# 把后台错误重定向到静态的50x.html页面
# redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
#如果访问的是.php结尾会把请求转发给http://127.0.0.1; # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #}
#执行时PHP服务器 如果访问的是.php结尾会把请求转发给127.0.0.1:9000; # 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; # } #} }
具体配置实验如下:
vue.config.js的配置有
devServer: { host: "0.0.0.0", port: 8080, index: "login.html", proxy: { ‘/api‘: { target: "http://localhost:7001" }, ‘/documents/‘: { target: "http://localhost:7001" } } },
nginx的配置:
server { listen 80 ; location ^~ /api { //对应vue的配置 proxy_redirect off; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_pass http://backend; } location ^~ /documents { //对应vue的配置 proxy_redirect off; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://backend; } location / { root C:\skyland\VIM\dist; //打包的dist文件放置的位置 index login.html; } location ~ ^/mediaUrl { rewrite ^/mediaUrl/(.*)$ /$1 break; root C:/InspectionFile/; } location ~ ^/bsFile{ //上传文件的储存位置 rewrite ^/bsFile/(.*)$ /$1 break; root C:/VIMUploadFiles/; } location ~ ^/StationImg{ rewrite ^/StationImg/(.*)$ /$1 break; root C:/VIMUploadFiles/; } location ~ /ISAPI|SDK/ { if ($http_cookie ~ "webVideoCtrlProxy=(.+)") { proxy_pass http://$cookie_webVideoCtrlProxy; break; } }
原文:https://www.cnblogs.com/Jennyishere/p/12956137.html