配置nginx相同端口多站点,首先我们要配置环境,这里用的是lnmp一键安装包,用一键安装包的方法后续再写,先看看安装之后的,我们浏览一下安装完成页面,

wKioL1Xs5DXzIaFFAANHAF0ZmxA659.jpg

这个是默认环境页面,可以在配置文件里看到,

wKiom1Xs49GQKWkYAARnGlghTrE052.jpg

根据上图可以看出默认路径是/home/wwwroot/default,这是个默认的配置文件,我们可以把这个默认的配置文件拷贝到vhost目录下/usr/local/nginx/conf/vhost/,拷贝之后我们需要修改一下默认配置文件,编辑nginx.conf,在最低端include后面的改成/usr/local/nginx/conf/vhost/*.conf;,看看正确的编辑。

[root@Python ~]# vi /usr/local/nginx/conf/nginx.conf
        gzip_disable   "MSIE [1-6]\.";
        #limit_conn_zone $binary_remote_addr zone=perip:10m;
        ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.
        server_tokens off;
        #log format
        log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';
                access_log off;
server
    {
        listen 80;
        #listen [::]:80 default_server ipv6only=on;
        server_name www.lnmp.org;
        index index.html index.htm index.php;
        root  /home/wwwroot/default;
        #error_page   404   /404.html;
        include enable-php.conf;
        location /nginx_status
        {
            stub_status on;
            access_log   off;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        server_name www.lnmp.org;
        index index.html index.htm index.php;
        root  /home/wwwroot/default;
        #error_page   404   /404.html;
        include enable-php.conf;
        location /nginx_status
        {
            stub_status on;
            access_log   off;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }
        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }
        location ~ /\.
        {
            deny all;
        }
        access_log  /home/wwwlogs/access.log  access;
    }
include /usr/local/nginx/conf/vhost/*.conf;
}

然后就是在vhost目录下修改我们使用的站点了,把拷贝过来的站点复制一份,并修改不同的名称,这里我们用两个域名www.aa.com、www.bb.com为了区分我们就用aa.com.conf、bb.com.conf命名,我们需要把一些不要的内容删除掉,也可以先修改后复制命名,比较省事。就以aa.com.conf为例:

[root@Python vhost]# vi aa.com.conf
#user  www www;        #删除
worker_processes auto;   #删除
error_log  /home/wwwlogs/nginx_error.log  crit;
pid        /usr/local/nginx/logs/nginx.pid;   #删除
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;   #删除
events      #删除
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }
http                 #删除
    {
        include       mime.types;
        default_type  application/octet-stream;
        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;
        sendfile   on;
        tcp_nopush on;
        keepalive_timeout 60;
       tcp_nodelay on;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;
        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";
        #limit_conn_zone $binary_remote_addr zone=perip:10m;
        ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.
        server_tokens off;
        #log format
        log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';
                access_log off;

以上信息在aa.com.conf和bb.com.conf文件里都需要删除部分还有就是在低端的include也要删除,并且为了区分我们上传一个论坛使用两个域名访问两个不同的目录,一个是默认的lnmp目录,一个是网站目录,www.aa.com使用默认目录配置如下:

[root@Python vhost]# vi aa.com.conf 
error_log  /home/wwwlogs/nginx_error.log  crit;
#Specifies the value for maximum file descriptors that can be opened by this process.
server
    {
        listen 80;        #端口
        #listen [::]:80 default_server ipv6only=on;
        server_name www.aa.com;        #域名
        index index.html index.htm index.php;
        root  /home/wwwroot/default;        #访问跟目录
        #error_page   404   /404.html;
        include enable-php.conf;
        location /nginx_status
        {
            stub_status on;
error_log  /home/wwwlogs/nginx_error.log  crit;
#Specifies the value for maximum file descriptors that can be opened by this process.
server
    {
        listen 80;
        #listen [::]:80 default_server ipv6only=on;
        server_name www.aa.com;
        index index.html index.htm index.php;
        root  /home/wwwroot/default;
        #error_page   404   /404.html;
        include enable-php.conf;
        location /nginx_status
        {
            stub_status on;
            access_log   off;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        listen 80;
        #listen [::]:80 default_server ipv6only=on;
        server_name www.aa.com;
        index index.html index.htm index.php;
        root  /home/wwwroot/default;
        #error_page   404   /404.html;
        include enable-php.conf;
        location /nginx_status
        {
            stub_status on;
            access_log   off;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }
        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }
        location ~ /\.
        {
            deny all;
        }
        access_log  /home/wwwlogs/access.log  access;
    }

域名bb.com.conf配置

[root@Python vhost]# vi bb.com.conf 
error_log  /home/wwwlogs/nginx_error.log  crit;
#Specifies the value for maximum file descriptors that can be opened by this process.
server
    {
        listen 80;        #端口
        #listen [::]:80 default_server ipv6only=on;
        server_name www.bb.com;    #域名
        index index.html index.htm index.php;
        root  /home/wwwroot/upload;    #网站根目录
        #error_page   404   /404.html;
        include enable-php.conf;
        location /nginx_status
        {
            stub_status on;
            access_log   off;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }
        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }
        location ~ /\.
        {
            deny all;
        }
        access_log  /home/wwwlogs/access.log  access;
    }

配置好之后我们就测试一下,用另外一台电脑,在hosts文件里把两个域名指定到一个IP上,

[root@localhost ~]# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.170.134 www.aa.com
192.168.170.134 
www.bb.com

下面就测试一下两个域名所浏览的页面

aa.com.conf配置的页面

wKioL1Xs7G-QYGnLAAOZ1UTy8PY865.jpg

bb.com.conf页面配置

wKiom1Xs6l2AjGvFAAOy9l8_-VM735.jpg测试完成。