web123456

nginx+ssh realizes intranet penetration

nginx+ssh realizes intranet penetration

Projects built locally can only be built locallyserverRunning on a local computer, how can you access the outside world through the IP port of the cloud server?

Environments to prepare:

1. The project that you run locally (herevueProject as an example: http://localhost:8082)

2. One unitCloud Server, Alibaba Cloud, Tencent Cloud, etc.

3. Nginx has been installed on the cloud server. If nginx is not installed, please install it on Baidu. The installation process is relatively simple.

4. Domain name (can be without)

1. Log in to the cloud server first

ssh user@ip (user is the cloud server username, ip is the cloud service public network IP).

Enter the password to enter the server directory.

2. Enter the nginx installation directory. My nginx is installed in the /usr/local/nginx directory.

cd /usr/local/nginx

Run the command ls to view the nginx directory, and there will be a conf folder, cd conf enters the folder, and execute ls to view the folder directory. The default configuration file for nginx.

3. We execute the command mkdir defaultconf under the conf folder to create a defaultconf folder, which is used to store our additional nginx configuration. cd defaultconf enters this folder. Run the command touch to create our own nginx configuration file. Execute the command vi to enter the file compilation mode. Press the keyboard i key to enter editing mode, and then paste the following code into it

server {
  listen 8888;#The cloud server port you listen to here is the subsequent access, which can be set to a port number that does not exist on any server.
   server_name xx.xx.cn;# Fill in your domain name here, if there is no domain name, fill in localhost
  
   location/ {
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    
    proxy_pass http://localhost:8899;# Here8899It is equivalent to an identifier, which is related to the subsequent creation of the channel locally. You can set any value}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

At this point, press the keyboard esc to exit editing mode, and then enter:wq to exit file viewing mode.

Next we need to restart nginx, cd /usr/local/nginx/sbin,

Execute ./nginx -s reload. ./nginx -t Check nginx startup status.

Next, go back to the local area and enter the command ssh -vnNt -R 8899:localhost:8082 user@ip

8899 is the identifier configured by our nginx, localhost:8082 is the access address of our local project, user is the cloud server username, and ip is the cloud server public network address. Enter the cloud server login password. The following of the last few behaviors means success.

debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: remote forward success for: listen 8899, connect localhost:8082
debug1: All remote forwarding requests processed
  • 1
  • 2
  • 3

4. Next, we can open the local browser, enter our own cloud server public IP (if there is a domain name, you can also access it through the domain name) and port to access it, for example: 8888.

If the words "Invalid Host header" appear, don't panic. It is caused by the vue project devserver. We only need to add a disabledHostCheck: true property to the devserver.

Notice

The configuration and installation directory may be somewhat different due to the server environment and nginx version, but they are all similar. Take your time and make sure that the nginx environment is normal, and then check whether the configuration penetration configuration is correct.

2. If each running environment is normal, if the browser keeps spinning around for the first time, don’t worry and wait patiently.