随着前后端分离式开发越来越受欢迎,随之而来的前后端服务连接的问题也更加突出。
既然前后端服连接上存在一些问题,那么就会有针对这些问题的一系列的解决方案,而其中最为优秀的一种就是nginx服务代理。
目录
首先介绍一下nginx的HTTP服务代理。
前端开发的网页基本上是静态资源文件,甚至可以直接通过打开文件的方式进行访问html页面,而后端开发的API接口则必须通过服务才能连接,而前端页面所需要的数据都来自于后端提供的API接口,如果想要一个网页正常运行,则前后端配合开发是必不可少的环节。由于前端开发的页面与后端开发的API不再同一个服务下面,前端要想直接连接后端API服务就会出现跨域问题。当然解决跨域的方案有很多,这里就不一一赘述了,我们要说的通过nginx配置来解决前后端开发在数据资源连接上的问题,下面要说的是发布环境上的前后端资源连接问题,至于开发环境上的前后端数据资源连接上的问题,网络上有很多,而且很多脚手架工具均有对应的解决方案,这里不再赘述。
Windows发布环境与Linux发布环境上的nginx配置基本上大同小异。
Nginx 默认配置
要配置nginx配置,首先要找到nginx的配置文件,以windows为例,一般情况下nginx的配置文件默认会在安装路径下 /nginx/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 {
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 logs/access.log main;
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 html;
index index.html index.htm;
}
#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;
# }
#}
}
启动nginx只需要双击 nginx.exe 文件即可,然后在浏览器地址栏内输入http://localhost/ 即可看到 nginx 默认的 index.html 如下图所示:
Nginx 配置 HTTP
worker_processes 1;
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"';
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
Nginx 配置 WebSocket
# ngxin.conf
# user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# 自定义变量 $connection_upgrade
map $http_upgrade $connection_upgrade {
default keep-alive; # 默认为keep-alive 可以支持 一般http请求
'websocket' upgrade; # 如果为websocket 则为 upgrade 可升级的。
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /websocket/ {
proxy_http_version 1.1;
proxy_read_timeout 360s;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade; # 此处配置 上面定义的变量
# proxy_set_header Connection $connection_upgrade;
proxy_set_header Connection "Upgrade"; #配置连接为升级连接
}
}
}
未完待续。。。