原文链接:https://blog.csdn.net/zhezhebie/article/details/73459874
---------------------------------------------
配置文件下载地址:https://download.csdn.net/download/zengmingen/10462400
共用80端口的,要server_name不同。如果用域名,要配置 hosts。
---------------------------------------------------------
这两天自己编译lnmp环境,以前都是在windows下面用配置好了的环境,并没有遇到什么问题,今天自己去弄,问题多多,配置域名的时候就遇到很多问题:
网上的说法也是乱七八糟,各种超长配置,叫你复制粘贴。废话不多说,直接说我是怎么解决这些问题的:
1、修改主配置文件:
只用改一个地方:
在http模块里面加入你要引用的虚拟主机配置文件目录即可:
例如:include /etc/nginx/vhosts/*.conf;
http {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;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 2048;include /etc/nginx/mime.types;default_type application/octet-stream;include /etc/nginx/vhosts/*.conf; //就这里加一行就可以了
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
2、建立虚拟主机配置目录:
在/etc/nginx/
下面建立vhosts
文件夹,专门放置网站的配置文件。
贴一个我的配置上来:
/etc/nginx/vhosts/mytest.com.conf
- 1
server {listen 80 ; //注意这里,要把默认的那个default_server去掉,因为我们在下面要单独配置域名访问,所以这里不要留default_server,不然会报错。server_name mytest.com mytest111.com; //这里写你想设置的域名,可以写多个,与名之间用空格隔开root /mnt/share/mytest.com; //这里是你虚拟机的根目录,写绝对路径# Load configuration files for the default server block.location / {index index.php index.html index.htm; //这里配置默认访问的页面}location ~* \.php$ { //这里配置php解析.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;}error_page 404 /404.html; //默认的错误页面location = /40x.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
再来一个:
server {listen 80;server_name www.xx.com;root D:/phpStudy/WWW/api-xxx;index index.html index.htm index.php;location / {try_files $uri $uri/ /index.php?$query_string;}location = /favicon.ico {log_not_found off;}location ~ \.php$ {fastcgi_pass 127.0.0.1:9000;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
3、还想再建其他虚拟机,一样的,复制上面那个文件,修改我标注的的那几个地方即可!
4、虚拟机配置文件配置好了之后,还需要在linux下面的hosts文件下面加上以上域名,不然还是会访问外网的哦。
vim /etc/hosts
- 1
127.0.0.1 mytest11.com
127.0.0.1 mytest.com
- 1
- 2
- 3
5、如果我们是要在windows下面访问虚拟机里面的linux下面的网站,那么还要在windows下面配置hosts文件,所有的域名都指向linux服务器,例如:
192.168.2.111 mytest11.com
192.168.2.111 mytest.com
- 1
- 2
经过以上5步就肯定可以访问了,再不能的检查一次,还是不能解决问题的,直接给我留言。
6、遇到的问题:
nginx: [emerg] a duplicate default server for 0.0.0.0:80 in /etc/nginx/vhosts/
- 1
遇到这个问题,肯定是:
server {listen 80
- 1
- 2
这个地方80后面的东西都去掉,只留下端口号80,去掉就可以解决这个问题了。