Skip to content

nginx install ​

Installing on an Operating System ​

There are plenty of similar tutorials and documents, and Windows and Linux have different installation methods, so they are not covered in detail here. The Docker-based installation is mainly introduced below.

Docker Installation ​

提示

Before running the container, if there is no bridge network, create one first for inter-container communication: docker network create -d bridge fastbee-bridge

  1. Run the following command to install nginx. After installation, package the frontend and upload the contents of the dist folder to the /var/data/nginx/vue directory.
shell
docker run \
--name nginx \
--volume /var/data/nginx/vue:/usr/share/nginx/html \
--publish 80:80 \
--publish 443:443 \
--network fastbee-bridge \
--restart unless-stopped \
--detach \
nginx:stable

The command above runs the container in bridge network mode, which requires opening the corresponding ports such as 80 and 443. You can also use the command below, which runs the container in host network mode so the container shares the host machine's network settings. Using host mode may cause security risks — if the container itself has security vulnerabilities, it is vulnerable to attack.

shell
docker run \
--name nginx \
--net=host \
--volume /var/data/nginx:/usr/share/nginx \
--volume /var/data/nginx/nginx.conf:/etc/nginx/nginx.conf \
--volume /var/data/nginx/log:/var/log/nginx \
--restart unless-stopped \
--detach \
nginx:stable
  1. Enter the container by running: docker exec -it nginx /bin/sh

  2. Modify the configuration file — copy and execute the following directly; exit is used to leave the container

yml
tee /etc/nginx/nginx.conf <<-'EOF'
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    client_max_body_size 10m;

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

    # Redirect Http to Https
    # server {
    #     listen 80;
    #     server_name localhost;
    #     location / {
    #         rewrite ^(.*) https://$server_name$1 permanent;
    #     }
    # }

    server {
        listen      80;

        # The default SSL access port is 443
        listen 443 ssl;
        server_name  localhost;
        charset utf-8;

        # Path of the certificate file
        ssl_certificate /usr/share/nginx/ssl/fastbee.crt;
        # Path of the private key file
        ssl_certificate_key /usr/share/nginx/ssl/fastbee.key;
        ssl_session_timeout 10m;
        # Configure according to the following protocols       
        ssl_protocols TLSv1.2 TLSv1.3; 
        # Configure cipher suites as follows, following the openssl standard       
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_prefer_server_ciphers on;

        # Frontend
        location / {
            root   /usr/share/nginx/html;
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }		
        # Backend API
        location /prod-api/ {
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://localhost:8080/;
        }

        # Proxy wss connections to ws
        location /mqtt {
            proxy_pass http://localhost:8083/mqtt;
            proxy_read_timeout 60s;
            proxy_set_header Host $host;
            proxy_set_header X-Real_IP $remote_addr;
            proxy_set_header X-Forwarded-for $remote_addr;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'Upgrade';
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
EOF
exit
  1. Restart the container for the configuration to take effect: docker restart nginx

FastBee | fastbee.cn