Linux服务器yum安装nginx环境,配置一个域名访问多个项目

//yum安装
yum install -y nginx

//启动nginx服务
systemctl start nginx.service


//设置开机启动
systemctl enable nginx.service

//Nginx相关配置信息
 
/usr/share/nginx/html               //网站的根目录  存放项目的地方

/etc/nginx/conf.d/default.conf      //网站的默认配置文件地址


/etc/nginx/conf.d/                  //存放配置的目录,可以根据规则自定义创建nginx配置文件


/etc/nginx/nginx.conf               //Nginx配置文件

首先我们查看nginx.conf文件配置  

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;         //一个域名访问多个不同项目,该行很重要。
}

查看conf.d目录

 本目录下有三个文件,在默认的情况下会访问default.conf文件。本项目取消使用默认文件,自定义其他文件访问。 default._conf 本文件增加_是为了在程序读取文件时避开自动查找  *.conf格式的文件。属于多次一举。不必如此操作。

本项目为一个域名访问两个项目。我们现在看一下think.conf的配置。

server {
    listen       80;
    server_name   think.ceshi.com;      //二级域名
 
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
    root   /usr/share/nginx/html/think/public/;    //存放项目地址。(项目入口文件,本配置没有配置匹配入口文件,需要手动填写路径到入口文件)
    #if (!-e $request_filename){   
    #        rewrite ^/(.*) /index.php last;
    #}

    location / {
        index  index.php index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html/think/public/;      
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~* \.php$ {
        fastcgi_index   index.php;
        fastcgi_pass    127.0.0.1:9000;
        include           fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;  //配置很重要。程序会自动通过域名找配置文件。
        fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

在本配置当中的几个重点注意的地方:

        1、创建的think.conf文件名字。跟think.conf中二级域名需要相同。是一一对应的关系。

        2、文件配置好之后,需要在阿里云平台(我服务器是阿里云的)域名配置中,A解析记录二级域名。

        3、think是我的项目,因为没有配置自动找寻入口文件。所以root这个位置需要手动填写到入口文件处。

        4、一个域名多个项目访问,这种配置只是多种方法中的一种。个人认为这种也是最方便,简单,容易管理的一种。

        5、想创建第二个或者多个项目,只需要复制think.conf  修改文件名后,root项目地址需要改成其他需要访问的项目。server_name   修改换成其他的二级域名,并且在阿里云平台(我都服务器是阿里云的)二级域名需要增加A解析记录。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韩若轩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值