The first few days of work and the front-end department of Students encounter a problem: the front-end students call the server-side interface, the server-side interface gets no cookie. The project is a front-end separation, the server side uses spring MVC to provide Java interface, the front-end using nginx do reverse proxy to solve cross-domain problems, front-end framework using Vue.
At that time because of the ngix do not know, only know the front-end classmate use Nginx to do reverse proxy, in the test environment is configured to ip+ port number of way to access the interface, so mistakenly think is not through the domain name access interface, so the server-side interface is not access to cookies. Also take for granted the front-end classmate "Correct the error", after some troubleshooting, the ultimate culprit is Vue,vue is the first page load and Refresh page will be through the front-end virtual server and the backend server interface interaction, this is the server side of the interface call, The front-end classmate does not correctly send cookies on the front-end server to the backend server interface, causing the interface to get no cookies.
Now think about really ashamed, really ignorant fearless, so decided to learn to learn nginx. This article records one of my introductory experiments, the purpose of the experiment: To configure a nginx reverse proxy server in this machine, listen to Port 8089. Access to the http://localhost:8089, will be through the Nginx proxy, access to the purpose of http://localhost:8080.
Environment: 32-bit Windows, Tomcat, Spring MVC
Pre-Preparation: Install Tomcat and create a new spring MVC project, Access http://localhost:8080/, to open the Sping MVC project corresponding to the action, do not say here, details please see my blog:/http Www.cnblogs.com/cs-js/p/7295414.html
First step: Install Nginx
Download Link: https://kevinworthington.com/nginx-for-windows/
My machine installed in the C:\nginx directory, after the installation, you can see the folder inside the Strat-nginx and Stop-nginx two files, one is to start the Nginx service, one is to shut down the Nginx service. In addition, you can see conf this folder, Ngix configuration file nginx.conf is placed in this folder. Configuring the Reverse proxy server only requires adding a few configurations to the configuration file.
Step Two: Configure Nginx
Open the nginx.conf configuration file, you will see in the HTTP node, under the HTTP node has a default server child node, we do not have to control it, to do is to add a server node under the HTTP node, the specific configuration is as follows:
#虚拟主机的配置 server { #监听端口 listen 8090; #域名可以有多个, separated by a space, proxy access to the real server server_name localhost:8080; Error_page 404 /404.html; CharSet Utf-8; #后端的Web服务器可以通过X-forwarded-for Get the user real IP proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for; Proxy_set_header x-forwarded-server $host; #以下是一些反向代理的配置 #反向代理时候, set Nginx to carry the domain name to the Tomcat server Proxy_set_header Host $host; #允许客户端请求的最大单文件字节数 Client_max_ Body_size 10m; #对 "/" Enable reverse proxy location /{ #配置代理访问的真实uri proxy_pass http://localhost:8080; } }
In this way, a simple reverse proxy server is configured. is not very simple, here it is important to explain the meaning of the server node and the various configuration items inside it:
Server: Configure a virtual host, a server interface can listen to a port, Nginx can be configured to listen to multiple ports, do multiple reverse proxies, each reverse proxy needs to configure a server node.
Listen: The port to monitor is the port of the Nginx server that is accessed.
SERVER_NAME: Name of the real server, can be configured as ip+ port or domain + port
Location: Different ways of the request to the different processing mode, where I configured the "/", meaning is configured to "/" the request to do the forwarding
Proxy_pass: The URI that is forwarded to the real server. This is the Intercept http:localhost:8089/, and then forwards it to http://localhost:8080.
Summary, Nginx's use is very simple, only need simple configuration can complete high-performance reverse proxy and load balancing function, interested students can be in-depth research on the Internet, and then the good resources to share, my mailbox [email protected].
Simple use and configuration of 32-bit Windows Nginx