# Nginx
## Run nginx locally
Follow the steps to run the nginx locally so you can:
* verify if the config is valid after making changes
* verify if ssl certificate works and troubleshoot issues locally instead of deploying it to the real server
### Prerequisite
You need to install nginx locally
`brew install nginx`
After the instalation run `nginx` in the terminal and then open `http://localhost:8080` in the browser. You should see the default nginx welcome page - `Welcome to nginx!`
### Build the config
By default when we run the build script it will output all config files in /dist folder. After that the deployment script takes care of copying and moving files to the correct directories. The problem here is that there are some hardcoded paths in the config files so to run the nginx config locally we have 2 options:
* to edit the config files so they match our file structure
* to move the config file so they match the file structure used on the real server
It is possible to run the config with both approaches. I'll use the first approach (changing the paths) because it seems easier than creating global folders and move the config there.
So before building the config make the following changes:
1. Fix the ssl certificates path - find all places where the `/nginx_cfg/ssl` directory is used and replace it with the absolute path of the `/dist/ssl/cloud directory`
> Example - Find `/nginx_cfg/ssl` -> Replace with `/Users/atanasov/Desktop/work/officernd-nginx/dist/ssl/cloud`
2. Fix the upstream redirects - find all places where `api:3030`, `app:3030` and `reports:3030` are used and replace them with `localhost:3030`
3. Fix the portal config references - find all places where `nginx_cfg/portal/` is used and replace with the aboslute path of the local `/dist/portal/cloud/` folder
> Example - Find `nginx_cfg/portal` -> Replace with `/Users/atanasov/Desktop/work/officernd-nginx/dist/portal/cloud/`
4. Edit the main nginx config - `/config/nginx.config`
* remove the user directive on line 1 - `user nginx` - the "user" directive makes sense only if the master process runs with super-user privilege
* remove the pid directive on line 3 - `pid /var/run/nginx.pid;`
* remove the error_log directive on line 4 - `error_log /var/log/nginx/error.log warn;` - we don't need logging but we can just create the logging file and this will work.
* remove the access_log directive on line 23 - `access_log /var/log/nginx/access.log main;` - same as the error_log
* remove including the mime types on line 16 - `include /etc/nginx/mime.types;` - we don't really need this for testing purposes
* change the include path at line 49 from `include /etc/nginx/sites-enabled/*;` to the absolute path of the `/dist/admin/cloud/*` folder
> Example - From `include /etc/nginx/sites-enabled/*;` to `/Users/atanasov/Desktop/work/officernd-nginx/dist/admin/cloud/*`
For reference you can check my branch [atanasov/local-setup](https://gitlab.com/officernd/officernd-nginx/-/tree/atanasov/local-setup)
After the changes are made you can build the config by running
`npm install && grunt`
### Modify the /etc/hosts file
We will test the nginx with real domains (app.officernd.com, campus.officernd.com, smart.halkin.com, etc.) so we should redirect this domains to our local server.
Open the `/etc/hosts` file in your favorite text editor - `code /etc/hosts` and add the following lines at the end:
127.0.0.1 app.officernd.com
127.0.0.1 smart.halkin.com
127.0.0.1 campus.officernd.com
Now when you open one of those domains your request will be redirected to your local address.
> Don't forget remove those lines after your're done.
### Run the nginx
Just execute `nginx -c {absolute-path-to-dist-nginx-config-file}`
> Example: `nginx -c /Users/atanasov/Desktop/work/officernd-nginx/dist/nginx.conf`
If there are no errors in the terminal the nginx should be running.
### Run the OfficeRnD server
We need to run the server in production environment because:
* admin portal - in the dev configuration the login page is set to lcoalhost:3030 and you will aways be redirected there
* community portal - in the dev configuration the `communityPortalDomain` setting is set to false and this prevent the portal to open on custom domains
1. Change the DB connection string to the live backup
2. Run the server in production:
`NODE_ENV=production LOG_LEVEL=info grunt dev`
### Verify if all works
* open app.officernd.com - you should be able to successfully login and see the admin portal.
* open the community portal on org without custom domain - campus.officernd.com
* open the community portal on org with custom domain - smart.halkin.com
* verify that you are hitting the local server by observing the incomming requests in your terminal
* verify that there is a valid ssl certificate on all domains