nginx 入门配置

这个星期公司的定期分享内容是Nginx,于是就要写作业了。

一、动静分离

1、下载Windows 版本的Nginx,解压,放到C盘下。进入目录,然后按然shift键右键,打开命令行,输入:

start nginx

2、访问localhost,启动成功。

3、然后做如下配置:

4、启动tomcat,访问localhost:8080:

5、重启nginx:

nginx -s reload

6、访问localhost/index.jsp,静态文件全部找不到,页面跑版。

7、把tomcat的静态文件放到目录下, 再次访问localhost/index.jsp:

8、一个简单的动静分享就做完啦。

 

二、绑定多个域名

1、在配置文件和server同级的位置加上这一句

    include C:/nginx-1.9.1/conf/vhosts/*.conf;

2、在conf目录下新建一个vhosts文件夹,里面新建两个文件:

tomcat.conf

    server {
        listen       80;
        server_name  tomcat.ice.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://localhost:8080;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

star.conf

    server {
        listen       80;
        server_name  star.ice.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   "H:/repository/svn/4";
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

3、在host文件中加下以下代码:

127.0.0.1       star.ice.com
127.0.0.1       tomcat.ice.com

4、重启nginx

nginx -s reload

5、分别访问star.ice.com和tomcat.ice.com:

6、打完收工,关闭nginx:

nginx -s quit

 

三、负载均衡

1、搭三个服务器:

192.168.1.104;
127.0.0.1:8080;
192.168.188.132:8080;

2、配置nginx.conf, 没有用的已经省略掉了(这里可以配置各种策略,具体参考官方文档吧):

http {

	upstream  www.ice.com {
		server   192.168.1.104;
		server   127.0.0.1:8080;
		server   192.168.188.132:8080;
	}

	server
	{
		listen  80;
		server_name  www.ice.com;

		location / {
		       proxy_pass        http://www.ice.com;
		       proxy_set_header   Host             $host; 
           proxy_set_header   X-Real-IP        $remote_addr; 
           proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for; 
		}
	}

}

3、重新启动nginx:

nginx -s reload

4,往host文件中加入

127.0.0.1        www.ice.com

5、打开www.ice.com,可以刷出以下三个页面:

6、负载均衡的最基本配置也做完啦。。。

posted @ 2015-06-05 17:56  ICE_XUE  阅读(412)  评论(0编辑  收藏  举报