`

linux下 nginx 初探之反向代理及虚拟目录

 
阅读更多

本文是基本已经安装nginx的前提下,若是未安装 sudo aptitude install nginx 即可!


一 反向代理
配置文件默认是在 /etc/nginx/nginx.conf
最新nginx是通过include指令读取其他的配置文件

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

我们不需要改变这个自带的版本,有也不宜去改变。copy一份出来
cp /etc/nginx/nginx.conf  /home/inter12/base/config/  #这个可以是你自己的任何目录

vim /home/inter12/base/config/nginx.conf 添加server

http {
....
  server {
  
     listen 9090;
     server_name localhost
  }
  ... 
  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
....
}

 
先暂时不需要理会是什么意思,确认下配置文件是否正确。
nginx -t -c /home/inter12/base/config/nginx.conf
nginx: the configuration file /home/inter12/base/config/nginx.conf syntax is ok
nginx: configuration file /home/inter12/base/config/nginx.conf test is successful
则表明是OK的。每次修改配置文件后,都可以采用这个命令来进行检查!

那么启动服务
nginx -c /home/inter12/base/config/nginx.conf
查看服务是否启动
ps -ef | grep nginx
netstat -anpl | grep 9090

浏览器访问 localhost:9090 出现一个 404错误页面 下面是 nginx/1.1.19 的话,恭喜你,第一步已经完成了!
listen 9090            //监听9090端口
server_name localhost  //监听的IP是localhost ,这个地方可以写多个 server_name localhost inter12.iteye.com(需要修改你本地的/etc/hosts文件)

tips:
若是不注释include /etc/nginx/sites-enabled/*;的,那么两个配置都会生效。即
localhost:9090 访问回事一个404错误,因为我们还没有指定默认的跳转地址
localhost:80  访问到默认的nginx页面 : welcome to nginx!

到目前为止的配置,我们已经告诉nginx,若是碰到localhost:9090就给进行代理,下面就解决的问题是如何代理?
修改配置文件(一下所说的配置文件都是我们copy出来的那份文件,不是默认的那份配置文件).

 

http {

...
   server {
    ... 
      charset utf-8;
        
        location / {
          proxy_pass  http://wwww.dianping.com;                                                                                                                                                    
        }
    ...
  } 
}

 

重新启动:
sudo nginx -c /home/inter12/base/config/nginx.conf -s quit
sudo nginx -c /home/inter12/base/config/nginx.conf

或是:
sudo nginx -c /home/inter12/base/config/nginx.conf -s reload  // 这个命令好像存在点问题!

这个时候访问
localhost:9090/   就会跳转到 www.dianping.com
通过firebugs查看,这个跳转过程的状态码是301 .          

若是加上

  http {

...
   server {
    ... 
      charset utf-8;
        
        location /hh {
           rewrite  ^(.*) http://wwww.dianping.com;                                                                                                                                                        
        }
    ...
  } 
}

                                                                                                                                                                                况

是服务器间的跳转,那么我需要跳转到一个具体的页面是怎么处理呢?请看下面
localhost:9090/hh   就会跳转到 www.dianping.com/hh 这个hh还会带过去,同理采用proxy_pass也会把location后面的路径带过去!
通过firebugs查看,这个跳转过程的状态码是302 .

延伸的说,我们可以采用rewirte配置统一的错误页面

error_page 403 404 500 /error;
  
   location /error {
      rewrite  ^(.*) http://wwww.dianping.com/error;
 }

 

二 如何建立虚拟目录
存在两种方式alias和root.先看看alias怎么配置虚拟路径

location ~ ^/a/ {
   alias /home/inter12/base/temp/;
}

 
配置后一直报403错误,查了下资料,可能是两个原因。
1.一般情况下nginx的用户是www-data, 属于www-data用户组。网页文件的属主一般不是www-data, 有可能造成不能访问。解决的办法是将网页文件的属主加到www-data用户组中:
usermod -G www-data inter12 .

2.当访问的url是一个路径而不是一个文件时,因为nginx默认是禁止列目录的,所以可能造成403返回。解决办法是在配置文件中加入默认访问文件。
location / {
root   /www;
index  index.php index.html index.htm ;
 }

既然知道原因了,那解决起来就容易,简单的做法就是将/home/inter12/base/temp/这个目录赋予777的权限,另一种新建一个www-data用户将文件放在其根目录下。这里选择的是第二种:

useradd www-data -s /bin/bash -h /home/www-data 
cp /home/inter12/base/temp/hh.html  /home/www-data/

 

修改配置为:

location ^~/a/{
             alias /home/www-data/;
 }

 
http://localhost:9090/a/hh.html 就可以看到我们自己的页面。其实这个URL对应到文件中就是 /home/www-data/hh.html 会把location后面的/a/这个路径去除。

再看看root怎么配置虚拟路径

 location ^~/r/{
    root /home/www-data/;
 }

 
重新启动后访问http://localhost:9090/r/hh.html,OK页面出现。采用root方式对应的文件路径是 /home/www-data/r/hh.html 也就是说location后来的/r/这个,目录不会丢失。
这个也是采用alias和root的最大区别!
网上查了些资料,有些提出是若是 location / {}采用 root ,其他的 location /other {}采用alias。具体原因也未提,以后再深究。

几个要点
1.任何一个配置语句都需要分号(;)结尾。
2.采用alias的话必须配置成 location ^~/a/{ ,而不可以配置成 location ~^/a/{} 这样是访问不到的,网上很多资料都不知道是哪里乱抄过来,害人不浅,还是stackoverflow上的答案靠谱!对于采用root方式的话,前后到无所谓。
3.向最权威,官方的地方查资料。http://wiki.nginx.org/Chs  

再附上一个完整的nginx.conf吧:

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
	worker_connections 768;
	
}

http {

	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;

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	gzip on;
	gzip_disable "msie6";

	server {
     
	    listen 9090;
		server_name localhost;

		charset utf-8;

        # proxy_pass 301 
		location /pp {

           proxy_pass http://www.dianping.com;
		}
       
	    # rewrite 302 
		location /re/ {
        
           rewrite ^(.)* http://www.dianping.com;
		}

        # visit local file by alias 
		location ^~/a/{
		   alias /home/www-data/;
		}
        
        # visit local file by root 
		location ^~/r/ {
		   root /home/www-data/;
        }

	}

	#include /etc/nginx/conf.d/*.conf;
	#include /etc/nginx/sites-enabled/*;

}

 


tips:
301
 被请求的资源已永久移动到新位置,并且将来任何对此资源的引用都应该使用本响应返回的若干个 URI 之一。如果可能,拥有链接编辑功能的客户端应当自动把请求的地址修改为从服务器反馈回来的地址。除非额外指定,否则这个响应也是可缓存的。
 新的永久性的 URI 应当在响应的 Location 域中返回。除非这是一个 HEAD 请求,否则响应的实体中应当包含指向新的 URI 的超链接及简短说明。
 
302的状态码解释:
 请求的资源现在临时从不同的 URI 响应请求。由于这样的重定向是临时的,客户端应当继续向原有地址发送以后的请求。只有在Cache-Control或Expires中进行了指定的情况下,这个响应才是可缓存的。
 新的临时性的 URI 应当在响应的 Location 域中返回。除非这是一个 HEAD 请求,否则响应的实体中应当包含指向新的 URI 的超链接及简短说明。                                             

总结:
上面主要设置了nginx的HTTP核心模块(HTTP Core)
测试配置文件是否正确 :nginx -t -c /home/inter12/base/config/nginx.conf  
启动nginx            : nginx  -c /home/inter12/base/config/nginx.conf
停止nginx            : nginx  -c /home/inter12/base/config/nginx.conf -s quit  //正常退出
停止nginx            : nginx  -c /home/inter12/base/config/nginx.conf -s stop  //强制停止

 

0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics