参考资料:
https://www.easyswoole.com/Introduction/proxy.html
https://wiki.swoole.com/wiki/page/326.html
1.vue + swoole + nginx
nginx配置如下 多多入口配置:
server {
listen 80;
server_name 123.com;
root /mnt/hgfs/abc/Views;
autoindex on;
index index.html index.htm;
#charset koi8-r;
#access_log logs/host.access.log main;
#这段是配置ssl的
ssl on;
ssl_certificate /var/www/abcd/abcd.pem;
ssl_certificate_key /var/www/abcd/abcd.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
#如果后端是多个模块 如admin/home 这样配置
#这个是解决vue刷新后页面404问题
location /home {
try_files $uri $uri/ /home/index.html;
}
location /admin {
try_files $uri $uri/ /admin/index.html;
}
location / {
proxy_http_version 1.1;
proxy_set_header Connection "keep-alive";
proxy_set_header X-Real-IP $remote_addr;
if (!-f $request_filename) {
proxy_pass http://127.0.0.1:9501;
}
}
nginx 单入口配置
server {
listen 8080;
server_name api.com;
root /var/www/html/abc/20191126/html;
index index.html index.htm;
#location / :表示域名 访问之后直接展示的目录,比如直接渲染index.html
location / {
#index index.html index.htm index.php;
try_files $uri $uri/ /index.html;
}
#location /Manager :表示前端和后端进行接口交互时(标志就是Manager)被nginx转发至9501端口
location /Manager {
proxy_http_version 1.1;
proxy_set_header Connection "keep-alive";
proxy_set_header X-Real-IP $remote_addr;
if (!-f $request_filename) {
proxy_pass http://127.0.0.1:9501;
}
}
}
nginx proxy_pass 代理转发多台服务器
upstream biz {
server 192.168.10.78:9501 weight=1;
server 192.168.10.75:9501 weight=1;
}
server {
listen 80;
server_name aaa.com;
root /var/www/html/bb;
index index.html index.htm index.php;
location / {
proxy_http_version 1.1;
proxy_set_header Connection "keep-alive";
proxy_set_header X-Real-IP $remote_addr;
if (!-f $request_filename) {
proxy_pass http://biz;
}
}
}
如果是vue+apache+swoole:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] fcgi下无效
RewriteRule ^(.*)$ http://127.0.0.1:9501/$1 [QSA,P,L]
#请开启 proxy_mod proxy_http_mod request_mod
</IfModule>
2.vue在nginx/apache 代理下页面刷新问题
nginx:
#这个是解决vue刷新后页面404问题
location /home {
try_files $uri $uri/ /home/index.html;
}
#或者
try_files $uri $uri/ /index.html;
apache:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
3. nginx 代理websocket
https://www.nginx.com/blog/websocket-nginx/
#wsapp 标识是路由的第一层级, 一般路由是三个层级. 第一个是表文件夹; 第二个是表class; 第三个是表method
location /wsapp/ {
proxy_pass http://wsbackend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
4. 字体跨域
location ~* \.(eot|ttf|woff|svg|otf)$ {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
}