對於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;
                }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章