Port for setting security rules

On the console of the cloud service platform purchased by yourself, choose Security Rule in the cloud server to set the network access rule and set the port to a subdomain name instead of a common port

Start the service first

First, enable the secondary DNS server and set the port to be consistent with the security rules. For example, the enabled port 3000 can be directly accessed through the domain name xxx.com:3000

Configure nginx

Nginx.conf in the conf directory of the nginx installation directory. To open the

cd /usr/local/nginx/conf
Copy the code

edit

vi nginx.conf
Copy the code

Usage Situation 1:

Replace the current nginx site with another site that looks like this:

#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #... Omit server {listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; The location / {# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - modify the out -- -- -- -- -- -- -- -- -- -- # root HTML; # index index.html index.htm; proxy_pass http://localhost:3000; # add} # omit}Copy the code

The second case

Just a secondary domain name. The new content is as follows (note: the new content must be added after the first server, or it will become the first host site) :

#user nobody; worker_processes 1; #.. Omit code 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; } #.. Omit code} # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- to find the above} brackets, the end of the server in the following new -- -- -- -- -- -- -- server {listen 80; # port server_name api.xxx.cn; # domain location / {proxy_pass http://localhost:3000; # Proxy place 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; }} #.. Omit code}Copy the code

4. Restart nginx

Restart to load the configuration file

/usr/local/nginx/sbin/nginx -s reload 
Copy the code

Remember it’s a reboot, not a boot

/usr/local/nginx/sbin/nginx -s reload 
Copy the code

You can access it

The source code

Some of the comments are removed, so it’s easy to see. Just add a new one after server, and then change the parameters

#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; Location / {root HTML; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }} server {listen 80; # port server_name api.xxx.cn; Rewrite ^(.*)$https://$host$1 permanent; Location / {proxy_pass http://localhost:3000; # Proxy place 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; }}}Copy the code

【 the original address: https://lolku.cn/web/details/posts/38