NGINX 一台服务器,两个网址同时使用80端口

环境:阿里云服务器
OS:centos 7.2 64位
两个网址,一个www的,一个wx的

一、首先一键安装lnmp

wget -c http://soft.vpser.net/lnmp/lnmp1.4.tar.gz && tar zxf lnmp1.4.tar.gz && cd lnmp1.4 && ./install.sh lnmp

nginx、mysql、php安装路径:

/usr/local

安装完成后,lnmp环境的软件都已经设置了开机自启动。

二、把两个网站拷贝到/home/wwwroot目录下

[root@web local]# cd /home/wwwroot/
[root@web wwwroot]# ll
drwxr-xr-x 17 root root 4096 Feb 27 13:30 pc
drwxr-xr-x 12 root root 4096 Feb  8 10:27 wx

三、导入网站的mysql数据库,并修改数据库的root密码,保证网站可以正常连接数据库。

[root@web ~]# mysql -uroot -p
Enter password: 

mysql> source  /root/pc.sql
mysql> source  /root/wx.sql

mysql> update mysql.user set authentication_string=password('123qwe') where user='root';
mysql> flush privilege;

四、修改nginx配置,pc和wx的网址都可以通过80端口进行访问。

1、先配置www的网站

[root@web conf]# pwd
/usr/local/nginx/conf
[root@web conf]# cat nginx.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;
        access_log off;

server
    {
        listen 80 default_server;
        #listen [::]:80 default_server ipv6only=on;
        server_name _;
        index index.html index.htm index.php;
        root  /home/wwwroot/pc;

        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        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 ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log  /home/wwwlogs/access.log;
    }
include vhost/*.conf;
}

[root@web conf]# 

2、配置wx的网站,注意上面的配置文件nginx.conf最后有句话

include vhost/*.conf;

在/usr/local/nginx/conf/vhost目录下新建一个wx.conf文件,将nginx.conf文件里server后的一段复制黏贴进去。

[root@web vhost]# pwd
/usr/local/nginx/conf/vhost
[root@web vhost]# ll
total 4
-rw-r--r-- 1 root root 904 Jun  2 11:58 wx.conf
[root@web vhost]# cat wx.conf 
server
    {
        listen 80;
        #listen [::]:80 default_server ipv6only=on;
        server_name wx.abc.com;
        index index.html index.htm index.php;
        root  /home/wwwroot/wx;

        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        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 ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log  /home/wwwlogs/access.log;
    }

[root@web vhost]# 

需修改的字段:

        listen 80;
        server_name wx.abc.com;
        index index.html index.htm index.php;
        root  /home/wwwroot/wx;

3、重启nginx

[root@web sbin]# pwd
/usr/local/nginx/sbin
[root@web sbin]# ./nginx -s reload

当然最后还要在域名解析系统里配置下域名对应的IP。

那么以上,配置完成。

你可能感兴趣的:(linux)