对于nginx配置中listen 和server_name 的理解

当nginx接到请求后,会匹配其配置中的service模块,

匹配方法就是靠请求携带的host和port正好对应其配置中的server_name 和listen。

如果做过ip和域名绑定,ip和域名二者是对等的

比如:


server {
        listen 8080;
        server_name www.abc.com;
        access_log  /opt/htdocs/logs/abc.log combined;
        index index.html index.htm index.php;
        root /opt/htdocs/abc;
        if ( $query_string ~* ".*[\;'\<\>].*" ){
                return 404;
        }
        location / {
                try_files $uri $uri/ /index.php;
        }
        location ~ .*\.(php|php5)?$  {
                #fastcgi_pass unix:/dev/shm/php-cgi.sock;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
                }
 
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
                expires 30d;
                }
 
        location ~ .*\.(js|css)?$ {
                expires 7d;
                }
}

比如服务器的ip地址为:1.2.3.4,在域名www.abc.com和ip已经绑定的情况下,那么在上面的配置中的server_name无论是www.abc.com还是1.2.3.4,我们都可以通过1.2.3.4:8080或www.abc.com:8080来访问我们的网站

 

一、80端口是服务器提供网站访问服务的默认端口,在访问一个网站例如:www.9410.com.cn的时候,实际的完整地址是http://www.9410.com.cn:80,省略为www.9410.com.cn,这时候在做域名解析的时候完全不需要考虑端口的问题。

二、当用的不是默认端口的时候,比如服务器提供网页访问服务但用的端口是81的时候,就只能使用以下两种方式来解决了:

1,还是用域名指向功能,将域名直接解析到ip上,也就是在域名后加“:端口号”,即通过http://www.abc.com:81来访问;

2,改用域名url转发功能,假如ip是123.123.123.123,端口是81,那么设置www.abc.com转发到http://123.123.123.123:81。

 

如果不想使用第二种方法的url转发功能,域名又不想带有端口,可以采用下面的方法

 

server {
        listen 80;
        server_name www.abc.com;
        access_log  /opt/htdocs/logs/abc.log combined;
        index index.html index.htm index.php;
        root /opt/htdocs/abc;
        if ( $query_string ~* ".*[\;'\<\>].*" ){
                return 404;
        }
        location / {
                try_files $uri $uri/ /index.php;
        }
        location ~ .*\.(php|php5)?$  {
                #fastcgi_pass unix:/dev/shm/php-cgi.sock;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
                }

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

        location ~ .*\.(js|css)?$ {
                expires 7d;
                }
}

server {
        listen 1002;
        server_name 1.2.3.4;
        access_log  /opt/htdocs/logs/abc.log combined;
        index index.html index.htm index.php;
        root /opt/htdocs/abc;
        if ( $query_string ~* ".*[\;'\<\>].*" ){
                return 404;
        }
        location / {
                try_files $uri $uri/ /index.php;
        }
        location ~ .*\.(php|php5)?$  {
                #fastcgi_pass unix:/dev/shm/php-cgi.sock;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
                }

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

        location ~ .*\.(js|css)?$ {
                expires 7d;
                }
}

 

  • 4
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值