How can I use nginx on my local machine to redirect cnn.com to a 404? - macos

I want to redirect cnn.com on my local Mac OSX to a 404 page. Here is what I have currently, but cnn.com still works (and redirects to edition.cnn.com):
/etc/hosts
127.0.0.1 cnn.com
/usr/local/etc/nginx/nginx.conf
# some config
http {
# some config
server {
listen 80;
server_name cnn.com;
location / {
return 404;
}
}
# more config
}
# more config
Or, is there a better way to accomplish this using another service?

Related

PATH_INFO variable is always empty when use fastcgi on MacOS

I installed nginx via brew and wrote this simple config for my python app:
server {
server_name localhost;
listen 3000;
location / {
include fastcgi_params;
fastcgi_pass localhost:8082;
}
}
When I start my app on 8082 and try to make any request, I always get response for main page. I found the reason. Flask gets url for matching from enveron PATH_INFO. And in my case this variable is always empty.
I have this problem only on MacOS. This app works well on Linux with same nginx config. What is my mistake?

using nginx reverse proxy for docker container

I am using nginx on my ubuntu machine and setup 2 laravel application using docker and one wordpress website without docker
Application 1: localhost:8088
Application 2: localhost:8089
I wanted to achieve is that when someone open localhost so it opens wordpress website and if someone open localhost/app1 it opens application 1 and so on.
So I have created reverse proxy so that it can open my docker container application
This is what I have done
sudo nano /etc/nginx/sites-available/website
ln -s /etc/nginx/sites-available/website /etc/nginx/sites-enabled/
nginx -t
systemctl restart nginx
After doing so when I try to open localhost/app1 it shows 404 but it recognise its a laravel app but shows 404
Here is my /etc/nginx/sites-available/website file code
server{
listen 80;
server_name localhost;
root /var/www/html/wordpress;
location /app1/{
proxy_pass http://localhost:8088;
}
}
You can create a file named redirects.map inside the nginx folder of your application and add a mapping like
~^localhost/app1/(.*) localhost:8089/$1;
You should change nginx configure from
server{
listen 80;
server_name localhost;
root /var/www/html/wordpress;
location /app1/{
proxy_pass http://localhost:8088;
}
}
To
server{
listen 80;
server_name localhost;
root /var/www/html/wordpress;
location /app1 {
proxy_pass http://localhost:8088;
}
}

301 redirects in nginx on laravel homestead

I'm trying to configure some 301 permanent redirects for some pages that have been moved (to a sub-folder of '/local'). My local environment, the test server and the live server all run on nginx, which I am new to. My local environment is set up with Homestead and I am trying to get the redirects to work there first.
I have ssh'd to my homestead vm, and edited the file /etc/nginx/nginx.conf: I have added this server block within the http block:
server {
server_name local.kkds;
location /essex {
rewrite ^/essex(.*)$ /local/essex$1 permanent;
}
}
When I include the server_name and do sudo nginx -s reload, I get the message
nginx: [warn] conflicting server name "local.kallikids" on 0.0.0.0:80, ignored
The urls redirect but all pages display an nginx 404 page, except the homepage which displays 'Welcome to nginx' page.
If I comment out the server_name the rest of the site works as it should but the redirects don't happen. I also tried replacing the server name with ip address in my homestead.yaml file and with 0.0.0.0 but still the redirect are not happening.
Perahps I am placing the configuration in the wrong place? Can someone point me in the right direction?

#spinner{} element fails two worker when Nginx is the reverse proxy in front of Nitrogen

I have running my Nitrogen driven application directly however because i want to use Nginx load-balancing magic i found out that the progress notifier of Nitrogen, the Spinner is not showing at all. I followed the example as at Nitrogen configuration options - bottom of the page. The example code snippet at the link is shown below.
# My config for a site that I only want serving SSL content.
server {
listen 80;
server_name www.mysite.com, mysite.com;
access_log /var/log/nginx/mysite.com.access.log;
# rewrite all requests to be SSL
rewrite ^(.*) https://$host$1 permanent;
}
server {
listen 443;
server_name mysite.com www.mysite.com
access_log /var/log/nginx/mysite.ssl.access.log;
ssl on;
ssl_certificate ssl/mysite/mysite.com.crt;
ssl_certificate_key ssl/mysite/mysite.com.key;
ssl_client_certificate ssl/mysite/ca.crt;
location / {
# This installation is running on port 8021, as you can plainly see.
proxy_pass http://127.0.0.1:8000;
}
}
Without Nginx the spinner works fine. I am using Nitrogen over Yaws of release as stated in the RELEASE file [{release,"nitrogen","2.3.0-beta5","5.10.3",[...,...,...,...],permanent}]. I do not what I am not doing right.

nginx config files redirecting to subfolder

I'm currently trying to deploy a website in two directories. Despite the lot of documentations on it, I was not able to obtain the behavior that I wish. This is the architecture of the websites :
The main website page is stored in /opt/www/mainsite/index.html
The second webiste (working with CodeIgniter) is stored in /opt/www/apps/myapp/index.php
I wish configure nginx to obtain this behavior :
All requests in http must be redirect to https
www.mydomain.com must point to /opt/www/mainsite/index.html
www.mydomain.com/myapp must point to /opt/www/apps/myapp/index.php
currently, my config file contains :
# redirect http to https
server {
listen 80;
rewrite ^(.*) https://$host$1 permanent;
}
# main webiste
server {
listen 443;
# ssl elements...
root /opt/www/mainsite;
index index.html;
server_name www.mydomain.com;
location / {
try_files $uri $uri/ /index.html;
}
}
On this page I found all the informations to set the config file for CodeIgniter. But I don't know how to create the rules to point mydomain.com/myapp on the CodeIgniter folder and how to configure CodeIgniter in order to set the right configuration.
Is anybody can help me?
thanks in advance
You need http://wiki.nginx.org/HttpFastcgiModule to setup CodeIgniter.
Using 2 server blocks is better than using if block for redirect. See IF is Evil.
Don't use $host because that variable value is obtained from the request's HOST header, and can be easily faked. Always set a server_name directive and use that name instead.
Using "return 301" directive is better than a rewrite. Saving cpu time (regex is slow) and easy to follow. Note that a 302 redirect (rewrite...redirect) has side effect because 302 will turn all POST requests to GET requests, which is not good in your case.
You don't need try_files in the main site because the main site just serves static files. But you can use 'expires' directive to allow browser to cache the static files.
server {
listen 80;
server_name www.mydomain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443;
server_name www.mydomain.com;
# ssl elements...
location / {
root /opt/www/mainsite;
index index.html;
expires max;
}
location /myapp {
root /opt/www/apps/myapp;
# fastcgi module goes here...
}
}
server {
listen 80;
listen 443 ssl;
…
if ($scheme != "https") {
rewrite ^ https://$server_name$request_uri? redirect;
}
root /opt/www/mainsite/;
location /myapp {
root /opt/www/apps/myapp/;
}
}
You'd put whatever configuration that is necessary for your myapp within the location for myapp.
BTW, it is generally a bad idea to host multiple independent apps within a single Host, because of XSS concerns.

Resources