【搭建后台环境】nginx http 配置二级域名

391 阅读1分钟

一、安全规则设置端口

在自己购买云服务平台的控制台中,在云服务器中,选择“安全规则”设置入网规则,设置端口为子域名的端口,别使用平常使用的端口

二、先启动服务

先将二级域名服务器开启,端口设置为安全规则一致即可,比如开启的 3000端口,可以通过域名直接访问到,xxx.com:3000

三、配置nginx

在 nginx 安装目录下的conf目录下的 nginx.conf 。进行打开

cd /usr/local/nginx/conf

进行编辑

vi nginx.conf

使用情况一:

将当前nginx网站替换成其他的网站,内容如下:

#user  nobody;
worker_processes  1;

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


# .....省略
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
                # ----------------修改此出----------
                #root   html;
                # index  index.html index.htm;
                 proxy_pass http://localhost:3000;  # 添加上
        }
       # 省略
}

第二种情况

只是弄二级域名。新增的内容如下(注意:新增的必须在第一个server后面新增,不然就是跑第一个当主网站了):

#user  nobody;
worker_processes  1;

# .. 省略代码

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;
                # proxy_pass http://localhost:3000;
        }

        # .. 省略代码
    }
    # ------------------ 找到上面server结束的 } 括号,在下面进行新增  -------
   server {
        listen 80;  # 端口
        server_name api.xxx.cn; # 域名
        location / {
                proxy_pass http://localhost:3000; # 代理的地方
                 proxy_http_version 1.1;
                 proxy_set_header Upgrade $http_upgrade;
                 proxy_set_header Connection 'upgrade';
                 proxy_set_header Host $host;
                 proxy_cache_bypass $http_upgrade;
        }
   }

   # .. 省略代码

}

四、重新启动 nginx

重新启动加载配置文件

/usr/local/nginx/sbin/nginx -s reload 

记住是重启,不是启动

/usr/local/nginx/sbin/nginx -s reload 

就可以访问了

源代码

有些注释去掉了后,一看就明白了,只要在server 后面新增一个就可以了,然后再改改参数

#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;
        rewrite ^(.*)$  https://$host$1 permanent; #用于将http页面重定向到https页面
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    

    # 只能在第一个 server 后面新增,不能放在前面
    server {
        listen 80;  # 端口
        server_name api.xxx.cn; # 域名
        rewrite ^(.*)$  https://$host$1 permanent; #用于将http页面重定向到https页面
        location / {
            proxy_pass http://localhost:3000; # 代理的地方
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }
}

【原地址】: https://lolku.cn/web/details/posts/38