I'm trying to setup my nginx as a reverse proxy server.

I have jenkins and nginx installed in the same server.I'm trying to access jenkins on port 82 instead of port 8080.

For that what i have done till now is

  1. Go to /etc/nginx/conf.d and create a new file with eg: 'forward_jenkins.conf'
  2. Contents of forward_jenkins.conf . (This is a random snippet that i found in the internet.)

    server {
      listen 82;
      server_name foobar.net;
    
      location / {
        proxy_pass http://127.0.0.1:8080;
        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-Forwarded-Proto $scheme;
      }
    }
    
  3. Save the file and start nginx.

Below is the error i get

[root@localhost conf.d]# systemctl start nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

I tried the below command too.

[root@localhost conf.d]# nginx -t -c /etc/nginx/conf.d/forward_jenkins.conf
nginx: [emerg] "server" directive is not allowed here in /etc/nginx/conf.d/forward_jenkins.conf:1
nginx: configuration file /etc/nginx/conf.d/forward_jenkins.conf test failed

I didn't modify anything in my nginx.conf file

[root@localhost nginx]# cat nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

Please help me fix this issue.

output of status command

[root@localhost conf.d]# systemctl status nginx ● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: failed (Result: exit-code) since Mon 2017-12-11 20:41:37 IST; 6s ago Process: 2446 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=1/FAILURE) Process: 2445 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)

Dec 11 20:41:36 localhost.localdomain systemd[1]: Starting The nginx HTTP and reverse proxy server... Dec 11 20:41:37 localhost.localdomain nginx[2446]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok Dec 11 20:41:37 localhost.localdomain nginx[2446]: nginx: [emerg] bind() to 0.0.0.0:82 failed (13: Permission denied) Dec 11 20:41:37 localhost.localdomain nginx[2446]: nginx: configuration file /etc/nginx/nginx.conf test failed Dec 11 20:41:37 localhost.localdomain systemd[1]: nginx.service: control process exited, code=exited status=1 Dec 11 20:41:37 localhost.localdomain systemd[1]: Failed to start The nginx HTTP and reverse proxy server. Dec 11 20:41:37 localhost.localdomain systemd[1]: Unit nginx.service entered failed state. Dec 11 20:41:37 localhost.localdomain systemd[1]: nginx.service failed.

  • What does nginx -t generate? (It is incorrect to specify the supplementary conf file on the command line). – Richard Smith Dec 10 '17 at 10:17
  • i assume it gives a hint about the error – Vishnu Rajendran Dec 11 '17 at 14:45
  • Well "server" directive is not allowed here is not the error, but the consequence of specifying -c /etc/nginx/conf.d/forward_jenkins.conf incorrectly on the command line. I don't know if nginx -t will report an error, if it doesn't you've eliminated one possible cause. – Richard Smith Dec 11 '17 at 14:48
  • okay ignore this step.It was just a troubleshoot step. --> nginx -t -c /etc/nginx/conf.d/forward_jenkins.conf The below error came first even before i try any troubledhoot step. [root@localhost conf.d]# systemctl start nginx Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details. Any idea to resolve this? – Vishnu Rajendran Dec 11 '17 at 15:00
  • Edit your question and add the output from nginx -t, and quite possibly systemctl status nginx.service and journalctl -xe too. – Richard Smith Dec 11 '17 at 15:13

Couple of things to check: Is your firewall letting you in from port 82 and 8080? Is Jenkins reachable without your NginX config from the 8080th port?

What I would do is to remove your current NginX install and install it from the NginX official repository. I assume you've installed it from the epel-release, right? I don't really like that because things like this happened with me too sometimes. This way, you'll get another nginx.conf file that is more plain, without any server directives, therefore less confusing.

I'm pretty sure you config is good, it is just that your nginx.conf ruins it. Removing nginx and reinstalling it from nginxs official repo, might fix your issue. But here is my nginx.conf with some modifications according to your setup:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}


http {
    server_tokens off;

    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    gzip on;
    gzip_min_length 1100;
    gzip_buffers 4 32k;
    gzip_types text/plain application/x-javascript text/xml text/css;
    gzip_vary on;

    include /etc/nginx/conf.d/*.conf;
    #include /etc/nginx/sites-enabled/*.conf;
}

You may try to copy-paste this to your nginx.conf file (make sure you create a backup of the files always).

  • i could see a permision denied error for port 82. Dec 11 20:41:37 localhost.localdomain nginx[2446]: nginx: [emerg] bind() to 0.0.0.0:82 failed (13: Permission denied) Let me google on this. – Vishnu Rajendran Dec 11 '17 at 15:54

Seems like its issue with SELinux mode.

Tried the below and it worked.

1.Check current SELinux mode by running getenforce. If it says Enforcing, then temporarily set the mode to Permissive by running setenforce 0, and see if your application works afterwards.

Now, I'm trying to work this with SELinux ON

I tried the below steps from th internet.

yum install policycoreutils-python

semanage port -a -t http_port_t -p tcp 82

semanage port -l | grep ^http_port_t

[root@localhost nginx]# semanage port -l | grep ^http_port_t http_port_t tcp 82, 80, 81, 443, 488, 8008, 8009, 8443, 9000

I could see port 82 added successfully.

But when i try to access my jenkins server using http://192.168.5.129:82/ from my host machine ,i'm getting below error on browser.

502 Bad Gateway

Note:

I'm able to launch jenkins from http://192.168.5.129:8080/

also im able to access nginx webserver by http://192.168.5.129:80

and when i rever my set my SELinux Permissive, i'm able to access jenkins in port 82.

But I'm trying to work this with SELinux ON.

  • solved by entering the below command. setsebool -P httpd_can_network_connect 1 – Vishnu Rajendran Dec 11 '17 at 16:48

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.