Home > other >  I need to redirect http to https connection with nginx on rest service and need to test it on postma
I need to redirect http to https connection with nginx on rest service and need to test it on postma

Time:10-06

Here is my nginx.conf file What should i change to make it work and how to get certificate; I need to redirect http to https connection with nginx on rest service and need to test it on postman or soapui. Is there any difference in configuring nginx for website and for webservice?

user nginx;
worker_processes auto;
error log /var/log/nginx/error.log;
pid /run/nginx.pid;

#Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
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 {
        }
    }
     server {
    listen       80;

    server_name hostname_of_virtual_machine http://ipaddress:port;

    return 301 https://$ipaddress:port$request_uri;
}
# Settings for a TLS enabled server.

    server {
        listen       443 ssl http2 default_server;
        listen       [::]:443 ssl http2 default_server;
        server_name  hostname_of_virtual_machine;
        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 PROFILE=SYSTEM;
        #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 {
        }
    }
}

When i try to test service in postman via http connection post method it still doesn't get any information; Any idea what should i do?

CodePudding user response:

  1. First of all you need to remove the configuration codes after include /etc/nginx/conf.d/*.conf;

Note: The example below using Ubuntu 20.04 LTS

  1. Go to /etc/nginx/sites-available and create a new file myapp01 and put your configuration there.

cd /etc/nginx/sites-available

sudo vi myapp01

Refer below snippet:

upstream appname-server {
    server 127.0.0.1:8080;
}

server {
    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 appname.com;

    access_log /var/log/nginx/appname-access.log;
    error_log /var/log/nginx/appname-error.log;

    location / {
        proxy_pass http://appname-server;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_read_timeout 5m;
        proxy_send_timeout 5m;
}


    listen 443 ssl;
    ssl_certificate /path/to/your/ssl/cert.pem;
    ssl_certificate_key /path/to/your/ssl/cert_key.pem;
}


server {
    if ($host = appname.com) {
        return 301 https://$host$request_uri;
    }


    server_name appname.com;
    listen 80;
    return 404;
}
  1. Don't for get to add include /etc/nginx/sites-enabled/*; in nginx.conf. (Thanks to Drifter104 for notifying)

     http {
    
         ##
         # Basic Settings
         ##
    
         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;
    
         ##
         # SSL Settings
         ##
    
         ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
         ssl_prefer_server_ciphers on;
    
         ##
         # Logging Settings
         ##
    
         access_log /var/log/nginx/access.log;
         error_log /var/log/nginx/error.log;
    
         ##
         # Gzip Settings
         ##
    
         gzip on;
    
         ##
         # Virtual Host Configs
         ##
    
         include /etc/nginx/conf.d/*.conf;
         include /etc/nginx/sites-enabled/*;
     }
    
  2. Next, go to /etc/nginx/sites-enabled and create symbolic links for myapp01, refer instructions below.

Go to sites-enabled cd /etc/nginx/sites-enabled/

Create symbolic links for myapp01 ln -s /etc/nginx/sites-available/myapp01 .

  1. After that, test your nginx configuration using sudo nginx -t. If everything is successful, proceed to step 5.

  2. Reload nginx sudo systemctl reload nginx

Hope it helps you, cheers.

CodePudding user response:

I have SSL Cert but don't know how to use it and where to put it. This is my conf file for now, and after talking with a colleague he told me that i just need a truststore in this file but i don't know how to create it. Btw, is this good? Thanks

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 / {
            #proxy_pass https://40.127.129.131:9444/;
            #proxy_set_header Token $http_token;
    }

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

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}
server {
    listen 80;
    server_name ip.add.rre.ss;
    return 301 https://ip.add.rre.ss:9444/$request_uri;

    proxy_connect_timeout 60s;
    proxy_send_timeout 120s;
    proxy_read_timeout 120s;

    location / {
            proxy_pass https://ip.add.rre.ss:9444/;
    }

} P.S. Version of my OS is Rhel 8.2

  • Related