Nginx如何配置虚拟主机?

你或许还需要了解

适用于CentOS7操作系统,Nginx建立虚拟主机配置多站点的方法:

Nginx配置

建立主机配置文件存放目录

mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled

修改Nginx配置文件

vi /etc/nginx/nginx.conf

找到这一行

include /etc/nginx/conf.d/*.conf;

在它下面加一行

include /etc/nginx/sites-enabled/*.conf;

保存并退出

8X------------------------------------------------我是分割线--------------------------------

虚拟主机配置

建立子站点目录

mkdir -p /mnt/www/example1.com/public_html/

建立测试文件

vi /mnt/www/example1.com/public_html/index.php
<?php
echo "<h1>Welcome to example1.com!</h1>";
?>

建立站点配置文件

vi /etc/nginx/sites-available/example1.com.conf

OPTION #1: 建立站点配置文件

server {
    listen 80; 
    root /mnt/www/example1.com/public_html;
    index index.php index.html index.htm;
    
    server_name example1.com www.example1.com;
    
    location / {
        try_files $uri $uri/ /index.html;
    }
    
   
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /mnt/www/example1.com/public_html;
    }
    
    # pass the PHP scripts to PHP-FPM server listening on 127.0.0.1:9000
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

OPTION #2: 如果使用的是ProcessWire项目则可以使用以下Rewrite规则:

你可以阅读《 》了解NGINX的缓存配置。

fastcgi_cache_path /mnt/www/towait.com/nginx_cache levels=1:2 keys_zone=towait:100m inactive=30d max_size=10g;

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 text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";

server {
    listen 80; 

    # ----------------------------------------------------------------------------------------------
    # Site Config
    # ----------------------------------------------------------------------------------------------   

    root /mnt/www/towait.com/public_html;
    index index.php index.html index.htm;
    
    server_name www.towait.com towait.com;

    client_max_body_size 50m; 
    
	# -----------------------------------------------------------------------------------------------
	# Optional: Redirect users to the 'www.' version of the site (uncomment to enable).
	# For example: http://processwire.com/ would be redirected to http://www.processwire.com/
	# -----------------------------------------------------------------------------------------------

	if ($host !~* ^www\.) {
		rewrite ^(.*)$ $scheme://www.$host$1 permanent;
	}


	# -----------------------------------------------------------------------------------------------
	# Access Restrictions: Protect ProcessWire system files
	# -----------------------------------------------------------------------------------------------

	# Block access to ProcessWire system files
	location ~ \.(inc|info|module|sh|sql)$ {
		deny all;
	}

	# Block access to any file or directory that begins with a period
	location ~ /\. {
		deny all;
	}

	# Block access to protected assets directories
	location ~ ^/(site|site-[^/]+)/assets/(cache|logs|backups|sessions|config|install|tmp)($|/.*$) {
		deny all;
	}

	# Block acceess to the /site/install/ directory
	location ~ ^/(site|site-[^/]+)/install($|/.*$) {
		deny all;
	}

	# Block dirs in /site/assets/ dirs that start with a hyphen
	location ~ ^/(site|site-[^/]+)/assets.*/-.+/.* {
		deny all;
	}

	# Block access to /wire/config.php, /site/config.php, /site/config-dev.php, and /wire/index.config.php
	location ~ ^/(wire|site|site-[^/]+)/(config|index\.config|config-dev)\.php$ {
		deny all;
	}

	# Block access to any PHP-based files in /templates-admin/
	location ~ ^/(wire|site|site-[^/]+)/templates-admin($|/|/.*\.(php|html?|tpl|inc))$ {
		deny all;
	}

	# Block access to any PHP or markup files in /site/templates/
	location ~ ^/(site|site-[^/]+)/templates($|/|/.*\.(php|html?|tpl|inc))$ {
		deny all;
	}

	# Block access to any PHP files in /site/assets/
	location ~ ^/(site|site-[^/]+)/assets($|/|/.*\.php)$ {
		deny all;
	}

	# Block access to any PHP files in core or core module directories
	location ~ ^/wire/(core|modules)/.*\.(php|inc|tpl|module)$ {
		deny all;
	}

	# Block access to any PHP files in /site/modules/
	location ~ ^/(site|site-[^/]+)/modules/.*\.(php|inc|tpl|module)$ {
		deny all;
	}

	# Block access to any software identifying txt files
	location ~ ^/(COPYRIGHT|INSTALL|README|htaccess)\.(txt|md)$ {
		deny all;
	}

	# Block all http access to the default/uninstalled site-default directory
	location ~ ^/site-default/ {
		deny all;
	}

	# -----------------------------------------------------------------------------------------------
	# If the request is for a static file, then set expires header and disable logging.
	# Give control to ProcessWire if the requested file or directory is non-existing.
	# -----------------------------------------------------------------------------------------------

	location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|eot|woff|ttf)$ {
		expires 24h;
		log_not_found off;
		access_log off;
		try_files $uri $uri/ /index.php?it=$uri&$args;
	}

	# -----------------------------------------------------------------------------------------------
	# This location processes all other requests. If the request is for a file or directory that
	# physically exists on the server, then load the file. Else give control to ProcessWire.
	# -----------------------------------------------------------------------------------------------

	set $skip_cache 0;

    # POST requests and urls with a query string should always go to PHP
    if ($request_method = POST) {
        set $skip_cache 1;
    }  

    #if ($query_string != "") {
    #    set $skip_cache 1;
    #}  
 
    # Don't cache uris containing the following segments 自行修改此处参数,如后台地址和不需要缓存的目录
    if ($request_uri ~* "/backend/|/api/") {
        set $skip_cache 1;
    }  
 
    # Don't use the cache for logged in users or recent commenters
    if ($http_cookie ~* "wire_challenge") {
        set $skip_cache 1;
    }

	location / {
            try_files $uri $uri/ /index.php?it=$uri&$args;
	}

   
	# -----------------------------------------------------------------------------------------------
	# Pass .php requests to fastcgi socket
	# -----------------------------------------------------------------------------------------------

	location ~ \.php$ {


		# Check if the requested PHP file actually exists for security
		try_files $uri =404;

        # Fix for server variables that behave differently under nginx/php-fpm than typically expected       
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;

		# Caching Content
		fastcgi_cache_bypass $skip_cache;
		fastcgi_no_cache $skip_cache;

		fastcgi_cache towait;
		
		# Params
		fastcgi_cache_valid 200 301 302 12h;
		fastcgi_cache_valid 404 500 502 503 504 0s;
		fastcgi_cache_valid any 1m;
		fastcgi_cache_min_uses 1;
		fastcgi_cache_use_stale error timeout invalid_header http_500 http_503 updating;
		
                fastcgi_pass_header Set-Cookie;
                fastcgi_pass_header Cookie;
                fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

		add_header X-Cache "$upstream_cache_status - $upstream_response_time";
		fastcgi_cache_key "$scheme$request_method$host$request_uri";

	}


}

建立软连接

 ln -s /etc/nginx/sites-available/example1.com.conf /etc/nginx/sites-enabled/example1.com.conf

使用命令 /usr/sbin/nginx -t 测试conf文件是否有误

[[email protected] public_html]# /usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

这点很重要!很重要!很重要!重要的事情说三遍,如果某个站点配置出错导致Nginx无法启动会使所有站点全部挂掉,到时候你会比我急,所以最好养成这个习惯,修改配置后先 /usr/sbin/nginx -t 一下!

最后重启Nginx

systemctl restart nginx

用你绑定的域名访问页面:

如添加更多站点只需要重复分割线以后的步骤即可

Post Comment