Linux系统Nginx配置多个域名的2种方法详解

include /etc/nginx/conf.d/*.conf;

登录到 Linux 实例后,运行以下命令:

nginx -t

即可看到 nginx.conf 文件所在目录,

图片.png

然后运行命令:

cat /etc/nginx/nginx.conf

查看内容,查看里边是否引入外部文件或者配置过其他项目,防止改错影响其他项目。

方法一:在 nginx.conf 文件里 server

{ 里边追加新项目内容,参考以下代码(只看 server{ 里边的内容):

server
{
listen 80; #监听端口设为80。
server_name test.yj521.com; #绑定您的域名。
index index.htm index.html index.php; #指定默认文件。
root /home/www/new_test_yj521; #指定网站根目录。
include location.conf; #当您需要调用其他配置文件时才粘贴此项,如无需要,请删除此项。
}

方法二:用includ导入文件的形式(方便各种域名管理)

当我们查看 nginx.conf 文件内容的时候里边有 :include /etc/nginx/conf.d/*.conf; 如图:

[图片上传失败...(image-737b07-1663957439333)]

代表着 nginx.conf 加载了外部文件,相当于把外部的文件内容加载过来

1、这时运行以下命令切换到指定目录下:

cd /etc/nginx/conf.d

如图:

图片.png

2、创建个名为“test-yj521.conf”文件准备把配置内容写入进去,运行创建文件命令:vim test-yj521.conf 实际文件名根据自己自定义。

3、写入配置内容,比如把以下内容复制过去:

server {
listen 80;
server_name test.yj521.com;
set root_path /data/rear-end/new-test-yj521/public; rootroot_path;
index index.php index.html index.htm;
}

详细 location 相关配置根据实际情况自行配置,比如我的配置:

server {
listen 80;
server_name test.yj521.com;
set root_path /data/rear-end/new-test-yj521/public; rootroot_path;
index index.php index.html index.htm;
location / {
if (!-e request_filename){ rewrite ^(.*) /index.php?s=1 last; break; } } location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index /index.php;
fastcgi_split_path_info ^(. .php)(/. )$;

    fastcgi_param PATH_INFO    $fastcgi_path_info;

    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    fastcgi_param TEST 1;

    include            fastcgi_params;

}
location ~* ^/(css|img|js|flv|swf|download)/(. )$ {
    root $root_path;
}

location ~ /\.ht {
    deny  all;
}

}

4、按 Esc 键退出编辑,输入

:wq #保存并退出

5、执行nginx -t命令,检查配置是否有误,并按照报错提示修复错误

6、执行如下命令

service nginx restart #重启Nginx服务

7、执行如下命令

service nginx reload #重新载入Nginx服务

配置完成,浏览器访问下配置的域名是否成功访问项目。

原文地址:https://www.o0310o.com/kb/531.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容