Nginx configure the implementation of multi port multi domain access
- 2020-05-17 07:52:11
- OfStack
The deployment of multiple sites on a server requires multiple ports to be opened to access different sites. The process is very simple. The debugging took 2 hours.
Master domain multi-port access
Set the A record at DNS NameServer
Point www.xxx.com to the server ip
Open the required port and modify the nginx configuration file
For example, we have two services open on port 80 and port 8080
If you have iptable, open the port first:
iptables -A INPUT -ptcp --dport 80 -j ACCEPT
iptables -A INPUT -ptcp --dport 8080 -j ACCEPT
Modify the configuration file:
#path: /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.xxx.com;
access_log /data/www/log/33.33.33.33_nginx.log combined;
index index.html index.htm index.php;
include /usr/local/nginx/conf/rewrite/none.conf;
root /data/www/website/33.33.33.33:80;
location ~ [^/]\.php(/|$) {
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
}
server {
listen 8080;
server_name A.xxx.com;
access_log /data/www/log/33.33.33.33:8080_nginx.log combined;
index index.html index.htm index.php;
include /usr/local/nginx/conf/rewrite/none.conf;
root /data/www/website/33.33.33.33:8080;
location ~ [^/]\.php(/|$) {
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
}
The key is the two server configuration sections, which you can also split into two configuration files and put them in
/etc/nginx/conf.d/
Below the table of contents;
Subdomain multi-port access
This access is silly, because you need the port 8080 to access http: / / xxx com: 8080 such format;
And if you have two different cgi, say port 80 for one php web service, port 8080 for one nodejs web service; Our nodejs comes with web service, which is already listening on port 8080. What should we do?
At this time, we need the reverse proxy function of Nginx, and add one A record above DNS Server, and finally realize it
www.xxx.com accesses port 80 A.xxx.com accesses the port 8080 service through nginx forwarding
Add 1 A record
Point A.xxx.com to the server ip
The Nginx configuration template is as follows:
#path: /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.xxx.com;
access_log /data/www/log/33.33.33.33_nginx.log combined;
index index.html index.htm index.php;
include /usr/local/nginx/conf/rewrite/none.conf;
root /data/www/website/33.33.33.33:80;
location ~ [^/]\.php(/|$) {
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
}
server {
listen 80;
listen [::]:80;
server_name A.XXX.com;
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
fastcgi_send_timeout 300s;
fastcgi_read_timeout 300s;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
try_files $uri $uri/ =404;
}
}
nginx reloads the configuration file
nginx -s reload