nginx配置多個站點共用80端口的解決方法
只用改一個地方,在http模塊里面加入你要引用的虛擬主機配置文件目錄即可:
例如:include /usr/local/nginx/default.d/*.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 /usr/local/nginx/default.d/*.conf; //就這里加一行就可以了 }
2、建立虛擬主機配置目錄:
在/usr/local/nginx/
下面建立default.d
文件夾,專門放置網(wǎng)站的配置文件。
貼一個我的配置上來:
/usr/local/nginx/default.d/mytest.conf
server { listen 80 ; //注意這里,要把默認的那個default_server去掉,因為我們在下面要單獨配置域名訪問,所以這里不要留default_server,不然會報錯。 server_name mytest.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 { } }
再來一個:
server { listen 80; server_name www.mytest1.com; root /var/www/html; 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; } }
3、還想再建其他虛擬機,一樣的,復制上面那個文件,修改我標注的的那幾個地方即可!
4、虛擬機配置文件配置好了之后,還需要在linux下面的hosts文件下面加上以上域名,不然還是會訪問外網(wǎng)的哦。
vim /etc/hosts 127.0.0.1 mytest1.com 127.0.0.1 mytest.com
5、如果我們是要在windows下面訪問虛擬機里面的linux下面的網(wǎng)站,那么還要在windows下面配置hosts文件,所有的域名都指向linux服務器,例如:
192.168.0.27 mytest1.com 192.168.0.27 mytest.com
6、遇到的問題:
nginx: [emerg] a duplicate default server for 0.0.0.0:80 in /etc/nginx/vhosts/
遇到這個問題,肯定是:
server { listen 80
這個地方80后面的東西都去掉,只留下端口號80,去掉就可以解決這個問題了。
總結
以上所述是小編給大家介紹的nginx配置多個站點共用80端口的解決方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
zabbix自定義監(jiān)控nginx狀態(tài)實現(xiàn)過程
這篇文章主要為大家介紹了zabbix如何自定義監(jiān)控nginx狀態(tài)的實現(xiàn)過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-10-10Nginx?502?bad?gateway錯誤解決的九種方案及原因
一般在訪問某些網(wǎng)站或者我們在做本地測試的時候,服務器突然返回502?Bad?Gateway?Nginx,這種問題相信大家也遇到不少了,下面這篇文章主要給大家介紹了關于Nginx?502?bad?gateway錯誤解決的九種方案及原因,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-08-08nginx-ingress-controller日志持久化方案的解決
這篇文章主要介紹了nginx-ingress-controller日志持久化方案的解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03