原文鏈接: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,去掉就可以解決這個問題了。