nginxhaproxynginx-config

Change port nginx to 81 but nginx still bind in port 80


I'm using nginx and haproxy, i want haproxy bind to port 80 so I'm change nginx to port 81. But when i change port nginx to 81 then start nginx still bind in port 80 and got error. What solution for me.

Here my nginx config default in /etc/nginx/sites-enabled

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
    listen 81;
    listen [::]:81;


    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass PHP scripts to FastCGI server
    #
    #location ~ \.php$ {
    #   include snippets/fastcgi-php.conf;
    #
    #   # With php-fpm (or other unix sockets):
    #   fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    #   # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#   listen 80;
#   listen [::]:80;
#
#   server_name example.com;
#
#   root /var/www/example.com;
#   index index.html;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}

And my haproxy config

global
    log /dev/log    local0
    log /dev/log    local1 notice
    chroot /var/lib/haproxy
    stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
    stats timeout 30s
    user haproxy
    group haproxy
    daemon

    # Default SSL material locations
    ca-base /etc/ssl/certs
    crt-base /etc/ssl/private

    # Default ciphers to use on SSL-enabled listening sockets.
    # For more information, see ciphers(1SSL). This list is from:
    #  https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
    # An alternative list with additional directives can be obtained from
    #  https://mozilla.github.io/server-side-tls/ssl-config-generator/?server=haproxy
    ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS
    ssl-default-bind-options no-sslv3

defaults
    log global
    mode    http
    option  httplog
    option  dontlognull
        timeout connect 5000
        timeout client  50000
        timeout server  50000
    errorfile 400 /etc/haproxy/errors/400.http
    errorfile 403 /etc/haproxy/errors/403.http
    errorfile 408 /etc/haproxy/errors/408.http
    errorfile 500 /etc/haproxy/errors/500.http
    errorfile 502 /etc/haproxy/errors/502.http
    errorfile 503 /etc/haproxy/errors/503.http
    errorfile 504 /etc/haproxy/errors/504.http
frontend myfrontend
    bind 127.0.0.1:80
    rate-limit sessions 50
    http-response set-header Cache-Control no-cache,\ max-age=600 if { capture.req.uri -m beg /api/ }
    capture request header Host len 32
    acl shipping-report.local hdr(host) -i shipping-report.local www.shipping-report.local/report
    use_backend report_shipping if shipping-report.local
    


backend report_shipping 
    server report01 shipping-report.local:8000

and I got error

nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Wed 2022-08-31 16:04:03 +07; 1min 0s ago
     Docs: man:nginx(8)
  Process: 30507 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=1/FAILURE)
  Process: 26443 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS)
  Process: 31737 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)
  Process: 31721 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 30483 (code=exited, status=0/SUCCESS)

Thg 8 31 16:04:01 falcon-anhnkd systemd[1]: Starting A high performance web server and a reverse proxy server...
Thg 8 31 16:04:01 falcon-anhnkd nginx[31737]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Thg 8 31 16:04:01 falcon-anhnkd nginx[31737]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Thg 8 31 16:04:02 falcon-anhnkd nginx[31737]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Thg 8 31 16:04:02 falcon-anhnkd nginx[31737]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Thg 8 31 16:04:03 falcon-anhnkd nginx[31737]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Thg 8 31 16:04:03 falcon-anhnkd nginx[31737]: nginx: [emerg] still could not bind()
Thg 8 31 16:04:03 falcon-anhnkd systemd[1]: nginx.service: Control process exited, code=exited status=1
Thg 8 31 16:04:03 falcon-anhnkd systemd[1]: nginx.service: Failed with result 'exit-code'.
Thg 8 31 16:04:03 falcon-anhnkd systemd[1]: Failed to start A high performance web server and a reverse proxy server.

What solution for me I want to use both nginx and haproxy


Solution

  • You may want to check the content of nginx.conf and as well conf/*.conf here. In my case, it was a default file in conf/ dir which was listening on port 80. Hope the answer helps.

    If you want to read about nginx vs haproxy, you can follow this post