配置多虛擬主機是每一臺WEB服務器幾乎會做的一個動作了,特別是我們這種小站長了,下文小編來為各位介紹lnmp環境nginx 配置多虛擬主機例子,希望例子可以幫助到大家.
1、首先進入 /usr/local/nginx/conf/ 目錄(自己nginx安裝的路徑),剛編譯好的nginx 在這個目錄下是木有 vhost 目錄的,創建好這個目錄后,打開nginx.conf 文件,在 http 范圍內添加 include vhost/*.conf,包含創建的虛擬主機配置文件,然后保存,創建虛擬目錄共用的server文件,就是每個conf都會使用到的配置項,我們把他獨立成一個模塊供大家使用.
server.conf文件:
- location ~ .*/.(php|php5)?$
- {
- #fastcgi_pass unix:/tmp/php-cgi.sock;
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- include fastcgi.conf;
- }
- location ~ .*/.(gif|jpg|jpeg|png|bmp|swf|ico)$
- {
- expires 30d;
- # access_log off;
- }
- location ~ .*/.(js|css)?$
- {
- expires 15d;
- # access_log off;
- }
- fastcgi_index: (nginx的默認首頁文件)
如果URI以斜線結尾,文件名將追加到URI后面,這個值將存儲在變量$fastcgi_script_name中.
例如:
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;
fastcgi_pass:指定FastCGI服務器監聽端口與地址,可以是本機或者其它.
用netstat -tln 查看端口使用情況:
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN
可以看到9000端口處于監聽狀態:另外的其他fastcgi配置,放入fastcgi.conf公用配置文件中,server.conf 來包含他.
fastcgi.conf文件相關配置項:
- ?fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#腳本文件請求的路徑
- fastcgi_param QUERY_STRING $query_string; #請求的參數;如?app=123
- fastcgi_param REQUEST_METHOD $request_method; #請求的動作(GET,POST)
- fastcgi_param CONTENT_TYPE $content_type; #請求頭中的Content-Type字段
- fastcgi_param CONTENT_LENGTH $content_length; #請求頭中的Content-length字段。
- fastcgi_param SCRIPT_NAME $fastcgi_script_name; #腳本名稱
- fastcgi_param REQUEST_URI $request_uri; #請求的地址不帶參數
- fastcgi_param DOCUMENT_URI $document_uri; #與$uri相同。
- fastcgi_param DOCUMENT_ROOT $document_root; #網站的根目錄。在server配置中root指令中指定的值
- fastcgi_param SERVER_PROTOCOL $server_protocol; #請求使用的協議,通常是HTTP/1.0或HTTP/1.1。
- fastcgi_param GATEWAY_INTERFACE CGI/1.1;#cgi 版本
- fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;#nginx 版本號,可修改、隱藏
- fastcgi_param REMOTE_ADDR $remote_addr; #客戶端IP
- fastcgi_param REMOTE_PORT $remote_port; #客戶端端口
- fastcgi_param SERVER_ADDR $server_addr; #服務器IP地址
- fastcgi_param SERVER_PORT $server_port; #服務器端口
- fastcgi_param SERVER_NAME $server_name; #服務器名,域名在server配置中指定的server_name
- #fastcgi_param PATH_INFO $path_info;#可自定義變量
- # PHP only, required if PHP was built with --enable-force-cgi-redirect
- #fastcgi_param REDIRECT_STATUS 200;
2、準備好了公用文件server.conf 和 fastcgi.conf 后,進入vhost目錄,之前手動創建的,創建虛擬主機.
- vim test.conf;
- server
- {
- listen 80;
- server_name test.cn;
- index index.html index.htm index.php;
- root /var/www/test;
- access_log /var/www/logs/test.log;
- error_log off;
- location / {
- try_files $uri $uri/ /index.php$uri?$args;
- }
- if (!-e $request_filename) {
- rewrite ^(.*)$ /index.php?s=$1 last;
- break;
- }
- include server.conf;
- }
test.conf 虛擬主機文件配置完成
重啟nginx:
記得先驗證測試后再重啟,否則會出現nginx 重啟不來.
測試:/usr/local/nginx/sbin/nginx -t
沒問題后進行重啟:
重啟:/usr/local/nginx/sbin/nginx -s reload
如果沒有域名來解析指定到自己的主機ip,可以直接把自己的主機ip 指到本地,編輯C:/Windows/System32/drivers/etc/hosts 文件就可,加入:
- <二、我的實例>
- --------- vhosts/led.conf
- server {
- listen 80;
- server_name www.led.com
- index index.html index.htm index.php;
- root /usr/local/vhost/led;
- #charset koi8-r;
- #access_log logs/host.access.log main;
- location / {
- root /usr/local/vhost/led;
- index index.html index.htm index.php;
- }
- #error_page 404 /404.html;
- # redirect server error pages to the static page /50x.html
- #
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- # proxy the PHP scripts to Apache listening on 127.0.0.1:80
- #
- #location ~ /.php$ {
- # proxy_pass http://127.0.0.1;
- #}
- location ~ .*/.(php|php5)?$
- {
- #fastcgi_pass unix:/tmp/php-cgi.sock;
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
- include fastcgi_params;
- }
- location ~ .*/.(gif|jpg|jpeg|png|bmp|swf)$
- {
- expires 30d;
- }
- location ~ .*/.(js|css)?$
- {
- expires 1h;
- }
- log_format access '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" $http_x_forwarded_for';
- }
- ---------------- nginx.conf
- user nginx nginx;
- worker_processes 8;
- error_log logs/error.log;
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
- pid /usr/local/nginx/nginx.pid;
- worker_rlimit_nofile 65535;
- events {
- use epoll;
- worker_connections 65535;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- #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 logs/access.log main;
- server_names_hash_bucket_size 128;
- client_header_buffer_size 32k;
- large_client_header_buffers 4 32k;
- client_max_body_size 8m;
- sendfile on;
- tcp_nopush on;
- #keepalive_timeout 0;
- keepalive_timeout 65;
- tcp_nodelay on;
- fastcgi_connect_timeout 300;
- fastcgi_send_timeout 300;
- fastcgi_read_timeout 300;
- fastcgi_buffer_size 64k;
- fastcgi_buffers 4 64k;
- fastcgi_busy_buffers_size 128k;
- fastcgi_temp_file_write_size 128k;
- gzip on; //Vevb.com
- gzip_min_length 1k;
- gzip_buffers 4 16k;
- gzip_http_version 1.0;
- gzip_comp_level 2;
- gzip_types text/plain application/x-javascript text/css application/xml;
- gzip_vary on;
- server {
- listen 80;
- server_name _;
- server_name_in_redirect off;
- location / {
- root /usr/share/nginx/html;
- index index.html;
- }
- }
- # 包含所有的虛擬主機的配置文件
- include /usr/local/nginx/conf/vhosts/*;
- }
新聞熱點
疑難解答