search
HomeOperation and MaintenanceNginxHow nginx solves cross-domain problems

How nginx solves cross-domain problems

Jun 12, 2019 am 10:24 AM
nginxCross 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;
        }
    }

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;
        }
    }

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;

The location of error.log on Mac:

/usr/local/Cellar/nginx/1.15.8/logs/error.log

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
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
Using NGINX: Optimizing Website Performance and ReliabilityUsing NGINX: Optimizing Website Performance and ReliabilityMay 09, 2025 am 12:19 AM

NGINX can improve website performance and reliability by: 1. Process static content as a web server; 2. forward requests as a reverse proxy server; 3. allocate requests as a load balancer; 4. Reduce backend pressure as a cache server. NGINX can significantly improve website performance through configuration optimizations such as enabling Gzip compression and adjusting connection pooling.

NGINX's Purpose: Serving Web Content and MoreNGINX's Purpose: Serving Web Content and MoreMay 08, 2025 am 12:07 AM

NGINXserveswebcontentandactsasareverseproxy,loadbalancer,andmore.1)ItefficientlyservesstaticcontentlikeHTMLandimages.2)Itfunctionsasareverseproxyandloadbalancer,distributingtrafficacrossservers.3)NGINXenhancesperformancethroughcaching.4)Itofferssecur

NGINX Unit: Streamlining Application DeploymentNGINX Unit: Streamlining Application DeploymentMay 07, 2025 am 12:08 AM

NGINXUnit simplifies application deployment with dynamic configuration and multilingual support. 1) Dynamic configuration can be modified without restarting the server. 2) Supports multiple programming languages, such as Python, PHP, and Java. 3) Adopt asynchronous non-blocking I/O model to improve high concurrency processing performance.

NGINX's Impact: Web Servers and BeyondNGINX's Impact: Web Servers and BeyondMay 06, 2025 am 12:05 AM

NGINX initially solved the C10K problem and has now developed into an all-rounder who handles load balancing, reverse proxying and API gateways. 1) It is well-known for event-driven and non-blocking architectures and is suitable for high concurrency. 2) NGINX can be used as an HTTP and reverse proxy server, supporting IMAP/POP3. 3) Its working principle is based on event-driven and asynchronous I/O models, improving performance. 4) Basic usage includes configuring virtual hosts and load balancing, and advanced usage involves complex load balancing and caching strategies. 5) Common errors include configuration syntax errors and permission issues, and debugging skills include using nginx-t command and stub_status module. 6) Performance optimization suggestions include adjusting worker parameters, using gzip compression and

Nginx Troubleshooting: Diagnosing and Resolving Common ErrorsNginx Troubleshooting: Diagnosing and Resolving Common ErrorsMay 05, 2025 am 12:09 AM

Diagnosis and solutions for common errors of Nginx include: 1. View log files, 2. Adjust configuration files, 3. Optimize performance. By analyzing logs, adjusting timeout settings and optimizing cache and load balancing, errors such as 404, 502, 504 can be effectively resolved to improve website stability and performance.

Deploying Applications with NGINX Unit: A GuideDeploying Applications with NGINX Unit: A GuideMay 04, 2025 am 12:03 AM

NGINXUnitischosenfordeployingapplicationsduetoitsflexibility,easeofuse,andabilitytohandledynamicapplications.1)ItsupportsmultipleprogramminglanguageslikePython,PHP,Node.js,andJava.2)Itallowsdynamicreconfigurationwithoutdowntime.3)ItusesJSONforconfigu

NGINX and Web Hosting: Serving Files and Managing TrafficNGINX and Web Hosting: Serving Files and Managing TrafficMay 03, 2025 am 12:14 AM

NGINX can be used to serve files and manage traffic. 1) Configure NGINX service static files: define the listening port and file directory. 2) Implement load balancing and traffic management: Use upstream module and cache policies to optimize performance.

NGINX vs. Apache: Comparing Web Server TechnologiesNGINX vs. Apache: Comparing Web Server TechnologiesMay 02, 2025 am 12:08 AM

NGINX is suitable for handling high concurrency and static content, while Apache is suitable for dynamic content and complex URL rewrites. 1.NGINX adopts an event-driven model, suitable for high concurrency. 2. Apache uses process or thread model, which is suitable for dynamic content. 3. NGINX configuration is simple, Apache configuration is complex but more flexible.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version