记录配置nginx访问时过滤地址

版权声明:本文为博主原创文章,未经博主允许不得转载。http://blog.csdn.net/flower_vip https://blog.csdn.net/flower_vip/article/details/70810320

如果想要通过访问某个URL到指定的ip地址也就是服务器,目前我知道的有三种方式

1.本地配置hosts  例如   127.0.0.1  www.xxx.com

2.如果购买了域名需要在运营商进行指定配置域名绑定的ip地址,如果想配置二级域名,还需要在DNS服务商上进行绑定

3.nginx做方向代理,输入url后跳转到要到达的某个域名或ip地址


下面列举nginx的一些反向代理的配置,记录一下方面以后查看

server {
	listen		80;
	server_name     wiki.lvzheng.com;
	location / {
	if ($uri = '/')
	{
		rewrite ^(.*)$ http://wiki.lvzheng.com/dokuwiki permanent;
	}
		proxy_pass http://t.3000g.net;
                        proxy_redirect  off;
                        proxy_set_header X-Real-IP $remote_addr;
                        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_set_header Host $http_host;

	}
	location /dokuwiki {
                proxy_pass http://t.3000g.net;
                        proxy_redirect  off;
                        proxy_set_header X-Real-IP $remote_addr;
                        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_set_header Host $http_host;

        }

        access_log  logs/wiki.access.log  main;
}
上面这段nginx配置含义:首先访问wiki.lvzheng.com时,如果输入的路径为wiki.lvzheng.com/,那么就匹配到第一个location 因为这个location匹配/

如果输入的路径为wiki.lvzheng.com/dokuwiki那么匹配到location /dokuwiki上。这里的第一个location中有个判断,如果是直接输入wiki.lvzehng.com的我们手动转发一次

并带上/dokuwiki并且后面要加上关键字permanent,这样访问wiki.lvzheng.com的会直接带上/dokuwiki进行跳转

server {
        listen       80;
        server_name  xxx.com www.xxx.com www.xxx.com;
        
		if ( $uri = "" ){
			rewrite /index.html permanent;
		}
	 	if ( $uri = / ){
                rewrite /index.html permanent;
        }
		location = / {

		rewrite http://www.xxx.com/index.html permanent;
		}
        location / {
                        proxy_pass http://t.xxx.net:9987;
                        proxy_redirect  off;
                        proxy_set_header X-Real-IP $remote_addr;
                        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_set_header Host $http_host;

        }     
	include /opt/hosts/*.conf;
        access_log  logs/host.access.log  main;
        error_page 404 http://www.lvzheng.com;
}
这里的也是进行对输入url进行判断,不过是先进判断,如果uri等于空或/时跳转到指定的页面,更上面的是一样的道理 不过如果有参数的话记得带上 permanent关键字

这里的include表示 如果我们nginx.conf配置文件很长可以分开写,然后include关键字进行合并


猜你喜欢

转载自blog.csdn.net/flower_vip/article/details/70810320