N05※nginx反向代理配置

1> 域名重定向

vi /usr/local/nginx/conf/vhost/google.cn.conf  # goole.cn为示例域名
upstream proxy {
   server 127.0.0.1:8085;
   server 127.0.0.1:8086;
}
location / {
    proxy_pass http://proxy; 
    proxy_redirect off;
    proxy_set_header        Host    $host;
    proxy_set_header        X-Real-IP       $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
    proxy_max_temp_file_size 0; # 关掉硬盘缓存,增加写速度
    # SSE 相关配置
    proxy_http_version 1.1;
    proxy_set_header Connection '';
  }
systemctl restart nginx

2> nginx 默认配置

  server {
    listen 80;
    server_name _;
    access_log /data/wwwlogs/access_nginx.log combined;
    root /data/wwwroot/default;
    index index.html index.htm index.php;
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    location /nginx_status {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      deny all;
    }
    location ~ [^/]\.php(/|$) {
      #fastcgi_pass remote_php_ip:9000;
      fastcgi_pass unix:/dev/shm/php-cgi.sock;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      expires 30d;
      access_log off;
    }
    location ~ .*\.(js|css)?$ {
      expires 7d;
      access_log off;
    }
    location ~ ^/(\.user.ini|\.ht|\.git|\.svn|\.project|LICENSE|README.md) {
      deny all;
    }
    location /.well-known {
      allow all;
    }
  }

3> 配置auth_basic登录认证

① 安装htpasswd

# ubuntu系统
sudo apt-get install apache2-utils
# centos系统
sudo yum -y install httpd-tools
参数说明
-c创建passwdfile.如果passwdfile 已经存在,那么它会重新写入并删去原有内容.
-n不更新passwordfile,直接显示密码
-m使用MD5加密(默认)
-d使用CRYPT加密(默认)
-p使用普通文本格式的密码
-s使用SHA加密
-b在命令行中一并输入用户名和密码而不是根据提示输入密码

② 生成密码

cd /usr/local/nginx/conf/passwd
# 执行上命令后会要求输入两次密码,./passwdfile 是在当前目录下创建密码文件passwdfile ,username即为需要设置的账号
htpasswd -c ./ip_passwdfile username
htpasswd -bc ./ip_passwdfile username userpasswd

③ 修改nginx配置

server {
  location ~ [^/]\.php(/|$) {
    #fastcgi_pass remote_php_ip:9000;
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
  }

  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
    expires 30d;
    access_log off;
  }

  location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
    deny all;
  }


  location / {
    #要求登陆认证
    auth_basic "Protected";
    #密码文件路径
    auth_basic_user_file /usr/local/nginx/conf/passwd/ip_passwdfile;
    #反向代理链接
    proxy_pass http://192.168.1.1:12560;
  }
}

4> avatar头像服务

① 修改nginx 配置文件

vi /usr/local/nginx/conf/vhost/jd.com.conf

location /
{
        expires 7d;
        proxy_pass http://gravatar.com;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #limit_conn one 20;
        #limit_rate 200k;
}

② 重启 nginx 服务

systemctl restart nginx

③ 更改avatar服务器网址

头像服务器:https://网址/avatar/

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享