linux下nginx配置项目域名,绑定多个域名,新增配置,修改已有的配置,删除配置
登录到 Linux 实例后,运行以下命令:nginx -t 即可看到 nginx.conf 文件所在目录,
然后运行命令: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; 如图:
代表着 nginx.conf 加载了外部文件,相当于把外部的文件内容加载过来
1、这时运行以下命令切换到指定目录下:cd /etc/nginx/conf.d 如图:
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;
root $root_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;
root $root_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、执行如下命令,重启Nginx服务:service nginx restart
7、执行如下命令,重新载入Nginx服务:service nginx reload
配置完成,浏览器访问下配置的域名是否成功访问项目。