关于nginx代理问题

说明:以前在阿里云上通过nginx进行了子域名解析代理

ps:子域名解析的条件:主域名(假如是vanilla.com)备过案,备案时绑定了某个服务器的ip(假如是1.2.3.1),

浏览器输入域名:www.vanilla.com ,跳到ip1.2.3.1服务器,访问服务器下目录为/root/www/里的内容

浏览器输入域名:he.vanilla.com,调到ip192.168.221.13:6767/app

           配置情况如下:

我的nginx是通过yum直接安装的

[root@he ~]# nginx -vnginx version: nginx/1.12.2
[root@he ~]# cd /etc/nginx[root@he nginx]# ls
conf.d fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf nginx.conf.rpmsave scgi_params.default uwsgi_params.default default.d fastcgi.conf.default koi-win win-utf mime.types.default nginx.conf.default scgi_params uwsgi_params fastcgi_params.default [root@he nginx]# 

打开nginx的配置文件,配置如下:

凡是注销的都被我删掉了,凡是我修改过的都加##号解释
[root@he nginx]# vim nginx.conf
user root;   ###我不想创建nginx用户,
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  www.vanilla.com;
        root         /root/www/;  ##主域名要访问的目录
	index	index.html index.js;  ###目录下的首页
        include /etc/nginx/default.d/*.conf;  
        location / {
        try_files $uri $uri/ /index.html; 
       }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

创建子域名的扩展配置文件。

[root@he nginx]# cd conf.d/
[root@he conf.d]# cat f1.conf 
server{
  listen 80;
  server_name he.vanilla.cn;
  index index.html index.jsp index.php;
  location /app {
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_read_timeout     3600;
    proxy_connect_timeout  240;
    proxy_set_header Host $host;
    proxy_set_header X-RealIP $remote_addr;
    proxy_pass http://191.168.221.13:6767/app;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }
}
重新启动nginx,使修改的配置生效
访问测试都没有问题

下面

想做nginx的负载均衡代理,老是遇到“会话过期”的问题,网上一直说是cookies问题……试了还是没有解决,过程如下。

[root@he nginx]# vim nginx.conf  
相同的内容省略,主要看代理组写的位置
http {  
    upstream nginx-servers{
         ip_hash;
          server 192.168.221.13:6767 weight=1;
          server 192.168.221.17:6767 weight=2;
       }
    include /etc/nginx/conf.d/*.conf;  
    server {  
          
    }  
} 

[root@wt-01 conf.d]# cat proxy.conf
server{
  listen 8686;
  index index.html index.jsp index.php;
  location /app {
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_read_timeout     3600;
    proxy_connect_timeout  240;
    proxy_set_header Host $host;
    proxy_set_header X-RealIP $remote_addr;
    proxy_pass http://nginx-servers/app; #####我觉得没写错
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }
}

web访问http://1.2.3.1:8686/app可以打开页面,但是会话过期,烦。



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值