Nginx之虚拟主机-yellowcong

14 篇文章 1 订阅

Nginx添加虚拟主机,解决的问题是,一台公网服务器上,有映射多个内网的主机而且这种方式相对于通过域名/aa 这种方式好,起码看起来感觉是一台新的服务器,这个虚拟主机,需要多个域名才能玩,一个域名,就搞不了了,只能通过/xx的方式了,配置的两步骤:1、配置nginx.conf 开启虚拟主机,2、配置*.conf虚拟主机文件。

1、配置nginx.conf,开启虚拟主机

在nginx.conf 配置中添加include vhost/*.conf;
这里写图片描述
创建文件目录mkdir -p /home/wwwlogs/
下面是完整的配置文件

user  www www;

worker_processes auto;

error_log  /home/wwwlogs/nginx_error.log  crit;

pid        /usr/local/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
#worker_rlimit_nofile 51200;
worker_rlimit_nofile 102000;

events
    {
        use epoll;
#        worker_connections 51200;
        worker_connections 102000;
        multi_accept on;
    }

http
    {
        include       mime.types;
        default_type  application/octet-stream;

        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

	proxy_ignore_client_abort on;
        fastcgi_ignore_client_abort on;

        sendfile   on;
        tcp_nopush on;

        keepalive_timeout 60;

        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 256k;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";

        #limit_conn_zone $binary_remote_addr zone=perip:10m;
        ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.

        server_tokens off;
        #log format
        log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
             	'$status $body_bytes_sent "$http_referer" '
             	'"$http_user_agent" "$http_x_forwarded_for" ' 
                '$request_time "$upstream_response_time" '
		        '"$upstream_addr" "$upstream_connect_time"';
		#'$pipe $msec';
		access_log off;

server
    {
        listen 80 default_server;
        #listen [::]:80 default_server ipv6only=on;
        server_name localhost;
        index index.html index.htm index.php;
        root  /home/wwwroot/default;

        #error_page   404   /404.html;
       #include enable-php.conf;

        location /nginx_status
        {
            stub_status on;
            access_log   off;
        }
	
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log  /home/wwwlogs/access.log  access;
    }
include vhost/*.conf;
}



2、创建vhost,添加虚拟主机

在nginx.conf文件目录下,创建vhost文件夹,配置在vhost的配置文件

在这里插入图片描述
配置二级域名的方式来设定虚拟主机
sso.yellowcong.com.conf

upstream  proxy_pool_devtools {
        server 192.168.156.44:8080 weight=10 max_fails=0 fail_timeout=10s;
        server 192.168.156.44:8081 weight=10 max_fails=0 fail_timeout=10s;

	keepalive 512;
}

server
	{
		listen 80;
		#listen [::]:80;
		server_name sso.yellowcong.com;
		index index.html index.htm index.php default.html default.htm default.php;
		root  /data/jeeroot/sso.yellowcong.com/ROOT;

		#error_page   404   /404.html;
		location / {
                        proxy_next_upstream error timeout http_500 http_502 http_503 http_504 http_404;
                        proxy_read_timeout 4s;

			proxy_http_version 1.1;
                        proxy_set_header Connection "";
                        proxy_connect_timeout 10;

			proxy_set_header Host $host;
	                #proxy_set_header X-Forwarded-For $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_pass http://proxy_pool_devtools;
        	}
		location ~ ^/(WEB-INF)/ {
        		deny all;
        	}

        location /nginx_status {
            stub_status on;
            access_log   off;
        }

        location = /favicon.ico {
        	log_not_found off;
            access_log off;
        }
        location = /robots.txt {
            log_not_found off;
            access_log off;
        }

        location ~/\. {
            deny all;
            access_log off;
            log_not_found off;
        }

        	location ~ /\.(ht|svn|git) {
            		deny all;
        	}

		access_log  /home/wwwlogs/sso.yellowcong.com.log  access;
	}

3、测试配置,并重启nginx服务

sbin/nginx -t

sbin/nginx -s reload

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狂飙的yellowcong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值