对于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;
                }
}
发布了25 篇原创文章 · 获赞 2 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/longjuanfengzc/article/details/88317450