LNMP Environment Nginx Configure multiple virtual host examples

Source: Internet
Author: User

1, first into the/usr/local/nginx/conf/directory (their own nginx installation path), just compiled a good nginx in this directory is the wood has vhost directory, the creation of this directory, open the nginx.conf file, in the HTTP range to add Include vhost/*.conf, which contains the created virtual host configuration file, and then saves, creates the virtual directory shared server file, is the configuration item that each conf uses, we separate him into a module for everyone to use.

server.conf file:

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: (Default home file for Nginx)


If the URI ends with a slash, the file name is appended to the URI, and the value is stored in the variable $fastcgi_script_name.


For example:


Fastcgi_index index.php;
Fastcgi_param Script_filename/home/www/scripts/php$fastcgi_script_name;

Fastcgi_pass: (Specify FASTCGI server listener port and address, can be native or otherwise:)
Use Netstat-tln to view port usage:


TCP 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN


You can see that port 9000 is in the listening state

Additional fastcgi configuration, put into fastcgi.conf public configuration file, server.conf to include his

fastcgi.conf file-related configuration items:

Fastcgi_param script_filename $document _root$fastcgi_script_name; #脚本文件请求的路径
Fastcgi_param query_string $query _string; #请求的参数, as of a app=123
Fastcgi_param Request_method $request _method; #请求的动作 (Get,post)
Fastcgi_param Content_Type $content _type; #请求头中的Content-type Field
Fastcgi_param content_length $content _length; #请求头中的Content-length field.
Fastcgi_param script_name $fastcgi _script_name; #脚本名称
Fastcgi_param Request_uri $request _uri; #请求的地址不带参数
Fastcgi_param Document_uri $document _uri; #与 $uri the same.
Fastcgi_param document_root $document _root; #网站的根目录. The value specified in the root directive in the server configuration
Fastcgi_param server_protocol $server _protocol; #请求使用的协议, usually http/1.0 or http/1.1.
Fastcgi_param gateway_interface cgi/1.1; #cgi version
Fastcgi_param server_software nginx/$nginx _version; #nginx version number, can be modified, hidden
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; #服务器名, domain name specified in the Server configuration server_name
#fastcgi_param path_info $path _info; #可自定义变量
# PHP only, required if PHP is built with--enable-force-cgi-redirect
#fastcgi_param Redirect_status 200;

2, ready the Common Files server.conf and fastcgi.conf, into the Vhost directory (previously created manually), create a virtual host.
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 Virtual Host File configuration complete

Reboot Nginx:

Remember to verify the test before restarting, otherwise there will be nginx restart.

Test:/usr/local/nginx/sbin/nginx-t

Reboot after no problem:

Reboot:/usr/local/nginx/sbin/nginx-s Reload

If there is no domain name to resolve the designated host IP, you can direct your own host IP to the local, edit the C:\Windows\System32\drivers\etc\hosts file can be. Join:


< II, my example >
---------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 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;
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;

}

}

# contains all the configuration files for the virtual host

include/usr/local/nginx/conf/vhosts/*;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.