Home Operation and Maintenance Nginx How nginx solves cross-domain problems

How nginx solves cross-domain problems

Jun 12, 2019 am 10:24 AM
nginx Cross domain

How nginx solves cross-domain problems

How to use Nginx to solve front-end cross-domain problems?

Preface

When developing static pages, applications similar to Vue, we often call some interfaces, these interfaces are likely to be cross-domain, and then the browser A cross-origin issue will be reported and no adjustment will be made.

The simplest solution is to set the browser to ignore security issues and set --disable-web-security. However, this method is fine for developing PC pages, but it will not work if it is a mobile page.

Solution

Use Nginx to forward the request. Write cross-domain interfaces as interfaces of the local domain, and then forward these interfaces to the real request address.

Give me an example

For example, we are developing a Vue application.

Original:

The debugging page is: http://192.168.1.100:8080/

The requested interface is: http://ni .hao.sao/api/get/info

Step 1:

The requested interface is: http://192.168.1.100:8080/api/get/ info

PS: This solves the cross-domain problem.

Step 2:

After installing Nginx, go to the /usr/local/etc/nginx/ directory (this is for Mac) and modify the nginx.conf file .

Step 3:

Comment out the default server configuration.

Add below:

   server{
        listen 8888;
        server_name  192.168.1.100;
 
        location /{
            proxy_pass http://192.168.1.100:8080;
        }
 
        location /api{
            proxy_pass http://ni.hao.sao/api;
        }
    }
Copy after login

After saving, start Nginx.

PS: You don’t need to know much about Nginx configuration, it’s very simple.

Step 4:

Visit: http://192.168.1.100:8888/

Done.

PS: Note that the accessed port is ‘8888’. If you have addresses in other domains, just continue to add location.

Error Demonstration

I didn’t quite understand the configuration of Nginx at first, so I thought it could be configured as follows.

    server{
        listen 8080;
        server_name  192.168.1.100;
 
        location /api{
            proxy_pass http://ni.hao.sao/api;
        }
    }
Copy after login

The reason why I write this is that I think it will allow Nginx to help me monitor 8080 requests, and then only forward matching requests. What I didn't realize is that Nginx needs to occupy port 8080 after writing this.

Since the port needs to be occupied, it can no longer be occupied by other processes with the same protocol, which results in the developed page being unable to be enabled with port 8080. After a colleague mentioned it, I remembered this matter, changed my thinking, and came up with the top method.

Summary

In fact, you can do this not only during development and debugging, but also in the production environment. After using Nginx to forward the request, the static page to be deployed does not need to be placed in the same domain as the request interface.

Appendix

If you find a problem with the request and find that it is an error reported by Nginx, you only need to find the error.log of Nginx to know the reason.

The prerequisite is to configure error.log in nginx.conf:

error_log  logs/error.log;
Copy after login

The location of error.log on Mac:

/usr/local/Cellar/nginx/1.15.8/logs/error.log
Copy after login

For more Nginx related knowledge, please visit Nginx usage tutorial column!

The above is the detailed content of How nginx solves cross-domain problems. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to check whether nginx is started? How to check whether nginx is started? Apr 14, 2025 pm 12:48 PM

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

How to start nginx in Linux How to start nginx in Linux Apr 14, 2025 pm 12:51 PM

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

How to solve nginx403 How to solve nginx403 Apr 14, 2025 am 10:33 AM

How to fix Nginx 403 Forbidden error? Check file or directory permissions; 2. Check .htaccess file; 3. Check Nginx configuration file; 4. Restart Nginx. Other possible causes include firewall rules, SELinux settings, or application issues.

How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

See all articles