search
HomeBackend DevelopmentPHP TutorialCentOS 7 installs Nginx as a reverse proxy

Title

You need to use the reverse proxy function of nginx. The test environment is centos+NGINX 1.8.0.

<code>跳过一些繁琐的问题,直接记录核心
</code>

Steps

<code>(1)centos 安装在VM中,因此需要注意网络连接问题
(2)安装nginx使用的是具有网络的yum功能
(3)配置centos防火墙,需要开启80 端口
(4)nginx 反向代理配置
(5)性能优化设置(后续工作...)
</code>

implementation

1. Install nginx with yum
First add the nginx source and test using the latest nginx 1.8.0

<code>rpm -ivh  http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm 
</code>

Execute the command:

<code>yum install nginx
service nginx start
</code>

If nothing goes wrong, enter 127.0.0.1:80 in the browser and you will see the nginx welcome interface.

2. Check the configuration of nginx

<code>rpm -ql nginx
此命令可以查看nginx的配置信息
</code>

3. Close the firewall and configure iptables

centos uses firewall to configure ports and networks by default, but most of the online information now uses iptables. In view of the sufficient information, iptalbes is used instead.

Static firewall rules using iptables and ip6tables
If you want to use your own iptables and ip6tables static firewall rules, then please install iptables-services and disable firewalld, enable iptables and ip6tables:

<code>yum install iptables-services
systemctl mask firewalld.service
systemctl enable iptables.service
systemctl enable ip6tables.service
</code>

After enabling iptables, you need to set the port and access rules.

<code>(1)编辑  /etc/sysconfig/iptables
(2)清空规则
(3)添加需要的规则
</code>

Example:

# Allow established or connected traffic
-A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
#Allow local loopback interface
-A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
#Allow external access to this machine
-A OUTPUT -j ACCEPT
# Allow access to the SSH port. If the port is modified, you can change the corresponding port number
-A INPUT -p tcp –dport 22 -j ACCEPT
#Allow access to port 80 (HTTP)
-A INPUT -p tcp –dport 80 -j ACCEPT
#Allow access to FTP ports: 21, 20
-A INPUT -p tcp –dport 21 -j ACCEPT
-A INPUT -p tcp –dport 20 -j ACCEPT
#Allow access to port 161 (SNMP):
-A INPUT -p udp –dport 161 -j ACCEPT


Based on the above configuration, websites can be accessed from each other in the LAN.

4. Configure the reverse proxy function of nginx

<code>本次只是使用反向代理功能,因此nginx的负载均衡功能就不涉及。
</code>

The reverse proxy function uses the proxy_pass and sub_filter modules

<code>location / {
    proxy_pass  需要代理的IP;

    #Proxy Settings
    proxy_redirect     off;
    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for
    proxy_max_temp_file_size 0;
    proxy_connect_timeout      90;
    proxy_send_timeout         90;
    proxy_read_timeout         90;
    proxy_buffer_size          4k;
    proxy_buffers              4 32k;
    proxy_busy_buffers_size    64k;
    proxy_temp_file_write_size 64k;
# 做反向代理时候,出现ip地址直接跳转,没有是使用代理IP ,是因为需要使用sub_filter.
sub_filter 需要代理的IP  nginx的本机服务器;
sub_filter_once off;    
   }
</code>

Summary:

nginx reverse proxy concept is relatively simple and easy to configure. The next step is to proceed Do a stress test to see the actual effect.


[1]http://www.centoscn.com/CentOS/Intermediate/2015/0313/4879.html Using iptables

[2]http://www.centoscn.com/CentOS/2013/0413/ 293.html Configure iptables ports and rules

[3]http://www.nginx.cn/927.html Reverse proxy

[4]http://zhaochen.blog.51cto.com/2029597/379233/

[5]https://github.com/yaoweibin/ngx_http_substitutions_filter_module

[6]http://www.xxorg.com/archives/3608

Copyright statement: This article is an original article by the blogger and has not been authorized by the author. Reprinting is not allowed with the permission of the blogger.

The above introduces the installation of Nginx as a reverse proxy on CentOS 7, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

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
How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

Simple Guide: Sending Email with PHP ScriptSimple Guide: Sending Email with PHP ScriptMay 12, 2025 am 12:02 AM

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP Performance: Identifying and Fixing BottlenecksPHP Performance: Identifying and Fixing BottlenecksMay 11, 2025 am 12:13 AM

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

Dependency Injection for PHP: a quick summaryDependency Injection for PHP: a quick summaryMay 11, 2025 am 12:09 AM

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

Increase PHP Performance: Caching Strategies & TechniquesIncrease PHP Performance: Caching Strategies & TechniquesMay 11, 2025 am 12:08 AM

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc

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 Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.