我在工作中主要用到的nginx功能就是前后端分离
#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 default_server;
#listen [::]:80 default_server;
server_name _;
root /usr/local/nginx/html;
# Load configuration files for the default server block.
include /usr/local/nginx/conf/default.d/*.conf;
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
include vhost/*.conf;
}
http {
server
{
listen 907;
server_name localhost;
root C:\Users\Administrator\Desktop\ccps_7_H-ui.admin.page_3.0;
#error_page 404 /404.html;
#access_log /data/logs/project/wallet-mobile-error.log main;
location ~* \.(html|htm)$
{
add_header Cache-Control no-store;
}
location ~* \.(html|gif|jpg|png|ico|js|css|otf|eot|svg|ttf|woff|swf|xlsx)$
{
root C:\Users\Administrator\Desktop\ccps_7_H-ui.admin.page_3.0;
}
location = /
{
root C:\Users\Administrator\Desktop\ccps_7_H-ui.admin.page_3.0;
}
location /
{
proxy_pass http://127.0.0.1:8080/blogs/;
}
}
}
这是一个最简单的nginx前后端分离的使用场景。
注意点:
1.解决nginx proxy_pass反向代理cookie,session丢失的问题
host、端口转换,cookie不会丢失
location /project {
proxy_pass http://127.0.0.1:8080/project;
}
路径也变化,则需要设置cookie的路径转换
location /proxy_path {
proxy_pass http://127.0.0.1:8080/project;
proxy_cookie_path /project /proxy_path;
}
直接代理本地端口
location /proxy_path {
proxy_pass http://127.0.0.1:8080/;
proxy_cookie_path /project /proxy_path; # project 为你的项目名 也可用变量代替
}
所以以上面的例子来说,为防止session丢失,那么只需要在配置文件的最后面加一句即可。
proxy_cookie_path /blogs/ /;
博客围绕nginx前后端分离使用场景展开,重点提及解决nginx proxy_pass反向代理时cookie、session丢失的问题。指出host、端口转换时cookie不会丢失,路径变化需设置路径转换,防止session丢失可在配置文件最后添加特定语句。
2074

被折叠的 条评论
为什么被折叠?



