binghai03 发表于 2016-12-23 07:04:32

nginx 安装及配置

1.安装gzip, 到http://www.zlib.net/ 下载gzip包,然后依次执行:
  tar -zxvf gzipXXX.tar.gz
  ./configure
  make
  make install
 
2.安装pcre包,到http://www.pcre.org/下载pcre包,然后依次执行:
  tar -zxvf pcreXXX.tar.gz
  ./configure
  make
  make intall
 
3.安装nginx, 下载源文件包,然后依次执行:
 tar -zxvf nginx.XXX.tar.gz
   ./configure
   make
   make install
 
4.nginx将被安装在 /usr/local/nginx中,打开 /usr/local/nginx/conf/nginx.conf,进行配置:
  server{
        listen       80;
        server_name    guoshanji.com;
        location / {
            proxy_pass http://localhost:8083;
            index index.html;
        }
    }
    server {
        listen       80;
        server_name  kevin.guoshanji.com;
        location / {
            proxy_pass http://localhost:8080;
            index index.html;
        }
    }
5.到 /usr/local/nginx/sbin/目录下执行如下命令:
 ./nginx -t  (测试配置是否正确)
   ./nginx    (启动nginx)
   ./nginx -s reload (重新启动,生配置文件生效)
   ./nginx -s stop (停止nginx)
 
注:如果启动报如下错误:
./nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
执行如下命令:
32位系统 # ln -s /usr/local/lib/libpcre.so.1 /lib
64位系统 # ln -s /usr/local/lib/libpcre.so.1 /lib64
 
备注2:
如果需要转发url,可进行如下配置,配置后请求url地址会被转发,但最后显示在地址栏的url会改变。
  server {
  listen       80;
  server_name  localhost;
location / {
         rewrite ^(.*)$ http://localhost:8080/distribute/distribute/execute.do?$request_uri? break;
        }        
  location /favicon.ico{
  }
  }
  备注3:
  转发请求到不同的服务器,并认状态,nginx.conf配置如下:
  worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;


    server {
        listen       80;
        server_name  localhost;
      


         
location / {

     if ($http_cookie ~* "website=old"){
     
     proxy_pass http://10.1.14.253:80;
     
     break;
         
         }
         if ($http_cookie ~* "website=new"){
         
         proxy_pass http://10.1.14.253:81;
         break;
         }
         
         
         proxy_pass http://localhost:8080;
         break;

        }
               
        
location /favicon.ico{
        }
    }

}
  注意需要将后端10.1.14.253:80,10.1.14.253:81的cookie写到客户端,下次请求时,将自动处于登录状态。
  根据pc浏览器或手机浏览器跳转到不同网站:
          location / {
          if ( $http_user_agent ~* "Mobile" ){
              proxy_pass   http://s.glocalme.com;
          }
          proxy_pass   http://en.glocalme.com;
          
        }
页: [1]
查看完整版本: nginx 安装及配置