nginx的重定向与反向代理

由于公司需求,近期修复了域名访问的问题,具体nginx配置如下
[html]  view plain  copy
  1. user  nginx;  
  2. worker_processes  1;  
  3.   
  4. error_log  /var/log/nginx/error.log warn;  
  5. pid        /var/run/nginx.pid;  
  6.   
  7.   
  8. events {  
  9.     worker_connections  1024;  
  10. }  
  11.   
  12.   
  13. http {  
  14.     include       /etc/nginx/mime.types;  
  15.     default_type  application/octet-stream;  
  16.   
  17.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  18.                       '$status $body_bytes_sent "$http_referer" '  
  19.                       '"$http_user_agent" "$http_x_forwarded_for"';  
  20.   
  21.     access_log  /var/log/nginx/access.log  main;  
  22.   
  23.     sendfile        on;  
  24.     #tcp_nopush     on;  
  25.   
  26.     keepalive_timeout  65;  
  27.   
  28.     #gzip  on;  
  29.   
  30.     include /etc/nginx/conf.d/*.conf;  
  31. server {  
  32.     listen 80;  
  33.     server_name www.xxx.cn xxx.cn;   
  34.     index index.html ;  
  35.         location ~* ^/(index|gsdt|clt|runman|gywm|fuwushang)\.html$ {     
  36.             rewrite ^.*$ /xxx/html/$request_uri redirect;   
  37.         }     
  38.     if ($host = 'xxx.cn') {  
  39.         rewrite ^/(.*)$ http://www.xxx.cn/$1 permanent;  
  40.     }  
  41.     location  /{  
  42.                 proxy_pass http://127.0.0.1:8080;  
  43.                 proxy_set_header Host $Host;  
  44.                 proxy_set_header X-Real-IP $remote_addr;  
  45.                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
  46.     }  
  47. }  
  48. server {  
  49.         listen          80 ;  
  50.         server_name www.xxx.com xxx.com;  
  51.         index index.html;  
  52.         location ~* ^/(index|gsdt|clt|runman|gywm|fuwushang)\.html$ {     
  53.             rewrite ^.*$ /xxx/html$request_uri redirect;   
  54.         }     
  55.     if ($host = 'xxx.com') {  
  56.         rewrite ^/(.*)$ http://www.xxx.com/$1 permanent;  
  57.     }  
  58.         location /{  
  59.                 proxy_pass http://127.0.0.1:8080;  
  60.                 proxy_set_header Host $Host;  
  61.                 proxy_set_header X-Real-IP $remote_addr;  
  62.                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
  63.     }  
  64.    }  
  65. }  

主要是做了xxx.cn和xxx.com域名的主页重定向和反向代理

第一段location控制了xxx.cn/index.html等具体几个文件的重定向,以解决百度快照和360快照内的访问问题

第二段if控制了无主机头访问时301永久重定向到www主机头的访问

第三段location控制了反向代理


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值