Skip to content

1台服务器、1个根域名,

server {
    listen 8002;
    # server_name localhost;
    #将请求转成https
    # rewrite ^(.*)$ https://$host$1 permanent;
    index index.html;

    location / {
        #网站主页路径。此路径仅供参考,具体请您按照实际目录操作。
        #例如,您的网站主页在 Nginx 服务器的 /etc/www 目录下,则请修改 root 后面的 html 为 /etc/www。
        root /home/www/demo; 
        # index  index.html index.htm;
    }
}

server {
    listen 80;
    server_name demo.newarea.site;
    location / {
        proxy_pass http://localhost:8002;
    }
}
server {
    listen 8001;
    server_name www.newarea.site;
    # index index.html;

    location / {
        #网站主页路径。此路径仅供参考,具体请您按照实际目录操作。
        #例如,您的网站主页在 Nginx 服务器的 /etc/www 目录下,则请修改 root 后面的 html 为 /etc/www。
        root /home/www/blog; 
        index  index.html index.htm;
    }
}

server {
    #SSL 默认访问端口号为 443
    listen 443 ssl; 
    #请填写绑定证书的域名
    server_name www.newarea.site; 

    #请填写证书文件的相对路径或绝对路径
    ssl_certificate /www/server/nginx/ssl/tencent/www.newarea.site_bundle.crt; 
    #请填写私钥文件的相对路径或绝对路径
    ssl_certificate_key /www/server/nginx/ssl/tencent/www.newarea.site.key;
    ssl_session_timeout 5m;
    #请按照以下协议配置
    ssl_protocols TLSv1.2 TLSv1.3; 
    #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; 
    ssl_prefer_server_ciphers on;

    
    location / {
        proxy_pass http://www.newarea.site:8001;
    }
}

server {
    listen 80;
    server_name www.newarea.site;
    #将请求转成https
    rewrite ^(.*)$ https://$host$1 permanent;
}

Windows

nginx                         
├─ conf                       
│  ├─ include                 
│  │  ├─ test.conf            
│  │  └─ test1.conf                        
│  ├─ nginx.conf              
│  ├─ nginx.conf.default                      
├─ html                       
│  ├─ test                    
│  │  └─ index.html           
│  ├─ test1                   
│  │  └─ index.html           
│  ├─ 50x.html                
│  └─ index.html              
└─ nginx.exe

主配置文件 conf/nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #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;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    include D:/soft/nginx/conf/include/*.conf;
}

conf/include/test.conf

server {
    listen 80;
    server_name test.jack.com;

    location / {
        root D:/soft/nginx/html/test;
        index index.html;
    }
}

conf/include/test1.conf

server {
    listen 80;
    server_name test1.jack.com;

    location / {
        root D:/soft/nginx/html/test1;
        index index.html;
    }
}

html/test/index.html

<p>test</p>

html/test1/index.html

<p>test1</p>

修改 host 文件,添加如下内容:

127.0.0.1 test.jack.com
127.0.0.1 test1.jack.com

此时部署了3个项目,分别是 http://test.jack.comhttp://test.jack.comhttp://localhost