Linux lnmp 配置虚拟域名 vhost

本文详细介绍了如何在Nginx中配置虚拟主机,包括设置监听端口、域名、项目根目录、错误页面以及PHP脚本的FastCGI传递。特别关注了如何正确配置pathinfo模式,确保访问不会出现404错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、首先假设我们的项目在 /home/wwwroot/default/bathroom  (bathroom 就是我们的框架项目)

具体过程如下:

1、cd /usr/local/nginx/conf/vhost 目录下

2、vim example1.com.conf 创建一个文件并把以下内容拷贝进去 ↓: 

server {

  listen 80;
  server_name example1.com www. example1.com;    #域名名称  以及二级虚拟域名
  #access_log /www/access_ example1.log main;   #访问记录日志存储位置

  location / {
    root /www/example1.com;            # 例如:root /home/wwwroot/default/Bathroom  (因为make安装的lnmp环境,默认项目根路径在/home/wwwroot/default/下访问)
    index index.php index.html index.htm;
  }

  error_page 500 502 503 504 /50x.html;

  location = /50x.html {
    root /usr/share/nginx/html;
  }
  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

  location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /www/example1.com/$fastcgi_script_name;  #这里需要改成/home/wwwroot/default/Bathroom/ (项目根目录)
    include fastcgi_params;
  }

  location ~ /\.ht {
    deny all;
  }
}

注意:可能你会想虚拟域名不是配置在nginx文件中吗。为什么会配置vhost下面

你可以  vim /user/local/nginx/conf/nginx.conf   配置文件

找到:,这里默认引入 vhost 下所有的文件,所以更方便管理

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

我的案例:

server {

    listen 80;
    server_name www.bathroomstation.com;
    #access_log /www/access_ example1.log main;
location
/ { root /home/wwwroot/default/Bathroom; index index.php index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
# location ~ \.php$ { $的意思是默认以.php文件结尾 location ~ \.php(.*) {
fastcgi_pass
127.0.0.1:9000; fastcgi_index index.php; # 配置pathinfo模式,访问不在出现404 fastcgi_param PATH_INFO $1; fastcgi_param SCRIPT_FILENAME /home/wwwroot/default/Bathroom/$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } }

 

转载于:https://www.cnblogs.com/lpblogs/p/7491969.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值