This article mainly introduces the configuration method of Nginx and Apache sharing port 80. Of course, if you want Nginx not to compete with Apache for port 80, the Nginx port modification method is also attached at the end of this article. Friends who need it can refer to it
A typical Nginx Apache application solution can be that Nginx occupies port 80, filters static requests, and then dynamically requests Proxy to Apache port 8080. The advantage of Proxy reverse proxy is that when accessing, it is always port 80, and visitors will not notice any difference.
But some applications are very "smart" and recognize that the port where Apache is located is 8080, and will add the following: 8080 to all relevant hyperlinks. If it's already dead, can I still have normal access? !
There is a way to solve this problem, which is to run apache on port 80. The same server has Nginx and Apache, two httpd services, both are 80, will there be no conflict?
The following is an example.
Nginx.conf configuration
server { listen 80; server_name www.webyang.net; }
Modify it.
server { listen 192.168.3.3:80; #指定Nginx只占用某个IP的80端口。 listen 192.168.10.3:80; #如果你服务器中有多个IP,还可以指定多个。 server_name www.webyang.net; }
If you have multiple virtual hosts in Nginx, each one needs to be modified like this.
Then it’s the turn of apache’s httpd.conf
Change the original
Listen 80
to
Listen 127.0.0.1:80
Same as Nginx, specify the IP and port occupied by apache.
Save and exit, restart apache to take effect.
If you also have multiple virtual hosts on apache. There is no need to modify them one by one like Nginx, as long as they are all on port 80.
Such as:
NameVirtualHost *:80 <VirtualHost *:80> ServerAdmin hello@abc.com DocumentRoot /data/web_server/admin ServerName www.webyang.net </VirtualHost>
Do you think that everything will be fine? No.
Such an apache can only be accessed through http://127.0.0.1:80, so it makes no sense for it to occupy port 80. It is better to use 8080 for apache and 80 for nginx.
So if your server has multiple IPs at this time, in addition to binding apache to 127.0.0.1, you can also bind the IP of another network card, then the problem will be solved.
But most people only have one independent IP, so this method is a mirage for many people.
Modify an idea, apache is still port 8080, modify the conf file of one nginx domain name
location / { try_files $uri @apache; } location @apache { internal; proxy_pass http://127.0.0.1:8080; } location ~ .*.(php|php5)?$ { proxy_pass http://127.0.0.1:8080; }
At this time, the domain name All actions go through Apache, including static files.
There are also many people who write this way:
upstream zend { server 127.0.0.1:8080; } location / { proxy_pass http://zend; 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_set_header X-Scheme $scheme; } location ~ .*.(php|php5)?$ { proxy_pass http://zend; 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_set_header X-Scheme $scheme; }
is roughly similar.
Nginx port modification
Modify the nginx.conf file implementation. The path of this file is /usr/local/nginx/conf/nginx.conf on Linux, and the installation directory \conf\nginx.conf on Windows.
server { listen 80; server_name localhost; …… }
is changed to
server { listen 81; server_name localhost; location / { root html; index index.html index.htm; } …… }
Of course, it is changed to 8080 , 8081 can be anything, it doesn’t have to be 81, but make sure iptable allows access to this port.
Note the location configuration:
root html; #根目录,相对于安装目录 index index.html index.htm; #默认主页
By default, you put the file in the html folder in the installation directory, and you can use Nginx access.
Related recommendations:
Comparison between Nginx and Apache
##
The above is the detailed content of Detailed explanation of the configuration method of Nginx and Apache sharing port 80. For more information, please follow other related articles on the PHP Chinese website!