跳至主要內容

nginx 创建虚拟站点

Mr.Lexon小于 1 分钟environment

nginx 创建虚拟站点

首先在配置文件夹中创建一个以对应要分流的站点域名为名称的配置文件,

touch example.com.conf

设定配置如下:

server {
    listen       80;
    server_name  example.com;

    #charset koi8-r;
    access_log  /usr/local/openresty/nginx/logs/example.com.log;

    location / {
        root   /usr/local/openresty/nginx/html/example.com;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html; #这里是为了能使vue运行的配置
    }
    #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   /usr/local/openresty/nginx/html/example.com;
    }

    # 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           /usr/local/openresty/nginx/html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}
    #location ~ ^/(images|img|javascript|js|css|flash|media|static)/ {
    #    root /usr/local/openresty/nginx/html/default/assets/;
    #    autoindex on;
    #    access_log off;
    #    expires 30d;
    #}
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

配置完成之后,对应在网站目录里面对应添加文件夹:

mkdir /usr/local/openresty/nginx/html/example.com;

然后将网站文件放进去重启nginx:

nginx -s reload

若使用docker部署,则需要在docker将容器重启

docker run \
-p 9002:80 \
--name nginx \
-v /home/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /home/nginx/log:/var/log/nginx \
-v /home/nginx/html:/usr/share/nginx/html \
-d openresty/openresty
上次编辑于:
贡献者: Lexon