Express.js/Nginx hidden rewrite rule? - php

So I am running an Express.js/Passport.js web server with Nginx on CentOS 7.
Here's the problem: I cannot display any endpoint that starts with w. Trying to do so results in a Cannot GET /wiki/Main_Page message. However, changing that same route from e.g. /welcome to /selcome works just fine.
Most likely culprit:
I installed Mediawiki but removed it not long after. IIRC there was some kind of setting that prettied up URLs that starting with /w after the domain. So I'm guessing a rewrite rule is persisting... just have no idea where.
Here are my config files:
/srv/node/example.com/app/routes/auth.js
module.exports = function(app, passport) {
//Changing the URLs below from welcome to selcome works fine
app.get('/activate', passport.authenticate('user-activate-account', {
successRedirect: '/welcome',
failureRedirect: '/404',
failureFlash: true
}));
...
app.get('/welcome', isLoggedIn, function(req, res) {
res.render('inside', {
page_title: 'Welcome!',
inc_style: true,
style_sheet: 'style/dashboard.css',
portal: function() {
return 'welcome';
}
});
});
};
/etc/nginx.conf
user nginx nginx;
error_log /var/log/nginx/error.log info; # [ debug | info | notice | warn | error | crit ]
events {
worker_connections 1024;
}
http {
include mime.types;
include /etc/nginx/sites_enabled/.conf; //Really *.conf but more readable this way
include /etc/letsencrypt/options-ssl-nginx.conf;
server_names_hash_bucket_size 64;
# Compression - requires gzip and gzip static modules.
gzip on;
gzip_static on;
gzip_vary on;
gzip_http_version 1.1;
gzip_min_length 700;
# Compression levels over 6 do not give an appreciable improvement
# in compression ratio, but take more resources.
gzip_comp_level 6;
# IE 6 and lower do not support gzip with Vary correctly.
gzip_disable "msie6";
# Before nginx 0.7.63:
#gzip_disable "MSIE [1-6]\.";
# Redirect http traffic to https
server {
listen 80 default_server;
server_name _;
return 301 https://$host$request_uri;
}
# Catch-all server for requests to invalid hosts.
# Also catches vulnerability scanners probing IP addresses.
server {
listen 443 ssl;
server_name bogus;
root /var/empty;
return 444;
location / {
try_files $uri $uri/ =404;
}
}
# If running php as fastcgi, specify php upstream.
upstream php {
server unix:/var/run/php7.2-fpm.socket;
}
}
/etc/nginx/sites_available/example.com.conf
#sub.example.com
server {
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
listen 443 ssl;
server_name sub.example.com;
access_log /var/log/nginx/sub.example.com.access.log;
error_log /var/log/nginx/sub.example.com.error.log;
location /something\.js {
alias /var/www/html/example.com/sub.example.com/design/;
location ~* \.(gif|jpe?g|png|svg|webm)$ {
try_files $uri =404;
expires 30d;
}
}
location / {
proxy_pass http://localhost:3847;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
So far I have
Purged Nginx config files, and every config file in the include tree, of any legacy rules.
Cleared Nginx cache
My node.js project folder does not even contain the string wiki anywhere except for NPM links in the node_modules folder.
All the PHP files in /etc, /etc/php.d, /etc/php-fpm.d do not contain the string wiki
Deleted mediawiki folder
Restarted nginx
Restarted php-fpm
Restarted entire machine
I'm genuinely baffled at where this problem could be. Any ideas?

Turns out it was the browser. Despite clearing history multiple times and using incognito mode, nothing changed. But using another browser worked flawlessly. I feel like an idiot, but at least I can finally put this rage-inducing mystery behind me.
Was using: Firefox (I guess private mode isn't enough)
Chrome showed it was a browser error.

Related

Problems parsing PHP code in HTML files with Nginx FastCGI?

I am trying to migrate my website from LAMP to NGINX FastCGI on Linux on AWS and I am having problems trying to parse PHP in some legacy .htm files on my site.
I have tried the solutions listed here:
HTML files as PHP in Nginx
php code inside html files not executed
NGINX execute embedded PHP in HTML file
Specifically, I am using:
location ~ \.(php|html|htm)$ {
and
security.limit_extensions = .php .htm .html
in my /etc/nginx/sites-available/mybrokensite.com and /etc/php-fpm.d/www.conf files.
When I open the .htm files in my browser, I just get a blank page. When I view source, I see the entire raw php and html in the file. If I rename the file with a .php extension it interprets the php and I get the formatted html file that I expect in my browser.
I used the following steps to setup my NGINX FastCGI Wordpress server:
https://gist.github.com/ericandrewlewis/95239573dc97c0e86714
Here is my config:
# Define the microcache path.
fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=microcache:100m inactive=60m;
# Redirect http traffic to https
server {
listen [::]:80;
listen 80;
server_name www.mybrokensite.com mybrokensite.com;
return 301 https://mybrokensite.com$request_uri;
}
# Redirect www https traffic to non-www https
server {
listen 443 ssl;
ssl_certificate_key /etc/sslmate/mybrokensite.com.key;
ssl_certificate /etc/sslmate/mybrokensite.com.chained.crt;
server_name www.mybrokensite.com;
return 301 https://mybrokensite.com$request_uri;
}
server {
listen 443 ssl;
server_name mybrokensite.com;
# Include defaults for allowed SSL/TLS protocols and handshake caches.
include h5bp/directive-only/ssl.conf;
# config to enable HSTS(HTTP Strict Transport Security) https://developer.mozilla.org/en-US/docs/Security/HTTP_Strict_Transport_Security
# to avoid ssl stripping https://en.wikipedia.org/wiki/SSL_stripping#SSL_stripping
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";
ssl_certificate_key /etc/sslmate/mybrokensite.com.key;
ssl_certificate /etc/sslmate/mybrokensite.com.chained.crt;
# Path for static files
root /sites/mybrokensite.com/public;
#Specify a charset
charset utf-8;
# Include the basic h5bp config set
include h5bp/basic.conf;
location / {
index index.php;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.(php|html|htm)$ {
fastcgi_cache microcache;
fastcgi_cache_key $scheme$host$request_method$request_uri;
fastcgi_cache_valid 200 304 10m;
fastcgi_cache_use_stale updating;
fastcgi_max_temp_file_size 1M;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
# Local variables to track whether to serve a microcached page or not.
set $no_cache_set 0;
set $no_cache_get 0;
# If a request comes in with a X-Nginx-Cache-Purge: 1 header, do not grab from cache
# But note that we will still store to cache
# We use this to proactively update items in the cache!
if ( $http_x_nginx_cache_purge ) {
set $no_cache_get 1;
}
# If the user has a user logged-in cookie, circumvent the microcache.
if ( $http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
set $no_cache_set 1;
set $no_cache_get 1;
}
# fastcgi_no_cache means "Do not store this proxy response in the cache"
fastcgi_no_cache $no_cache_set;
# fastcgi_cache_bypass means "Do not look in the cache for this request"
fastcgi_cache_bypass $no_cache_get;
}
}
My site is mostly a Wordpress site with some legacy .htm files with php in them. I am new to NGINX and any help would be greatly appreciated.

NGINX Index 404 Not Found

I've just installed a Ghost Blog on a new server running NGINX. The Ghost config.json file is pointing at the correct directory /blog and the blog loads fine when I visit it.
What isn't working is when I remove /blog from the URL, I get taken to a 404 page. I've checked my sites-enabled file, which looks like this:
server {
listen 80;
listen [::]:80;
server_name *********;
root /var/www/ghost/system/nginx-root;
location ^~ /blog {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://*********:2368;
proxy_redirect off;
}
location ~ /.well-known {
allow all;
}
client_max_body_size 50m;
But I'm not entirely sure what I need to change to not get the 404 error. I have an example .php file which should be loading but isn't.
I've always used the Digital Ocean One-Click Ghost app but I wanted to use the Ghost CLI this time round. I have a feeling I've missed something though.
the following may remove some of your restrictions but it will work
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
server_name _;
ssl on;
ssl_certificate /etc/letsencrypt/live/thedomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/thedomain.com/privkey.pem;
access_log /var/log/nginx/thedomain.access.log;
error_log /var/log/nginx/thedomain.error.log;
root /var/www/thedomain;
index index.html;
gzip on;
gzip_proxied any;
gzip_types text/css text/javascript text/xml text/plain application/javascript application/x-javascript application/json;
location / {
try_files $uri $uri/ =404;
}
}
You need to make sure all the ssl files are there and permissioned for access by www-data.
If you need to run certbot for the first time, just but the 443 code in an 80 block without the ssl statements
The nginx configuration you've posted only deals with Ghost.
You've setup a server responding on port 80, set the root to Ghost's nginx-root, and created 2 location blocks. One is for /blog/ and serves Ghost, the second .well-known block is for handling generation of SSL certificates with letsencrypt.
I'm not an expert at configuring nginx for PHP, but this guide from Digital Ocean and this stackoverflow question covers a lot of the details.
I think you have a couple of options:
Set the index to be index.php
Add a new location block for / which serves php files
Add a block to handle all php files
I believe adding a new location block like this, will mean any .php files you have will always be called if the path in the URL matches.
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
But the value of fastcgi_pass will depend on how you have PHP setup on your system.
With this, going to /index.php should work.
Setting index: index.php will mean that / maps to /index.php I'm not sure if that will interfere with Ghost, if it does, you'd need a specific location / {} block instead of the index being set.

Load meteor for the main domain and php code on subdomain with nginx

I have a challenge that I can't figure out how to solve.
I a DigitalOcean VPS running CentOS7. Here I host a domain, e.q. www.example.com.
I'm running nginx right now on it. It used to be Apache but I could not figure it out how to make the websockets reversed proxy, for meteor work with Apache.
Now in Nginx I created a vhost configuration to load for the main domain, www.example.com and example.com, from a meteor server that runs with pm2-meteor:
server_tokens off; # for security-by-obscurity: stop displaying nginx version
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 443 ssl spdy; # we enable SPDY here
server_name www.example.com; # this domain must match Common Name (CN) in the SSL certificate
root /var/www/html/example/bundle; # irrelevant
index index.html; # irrelevant
ssl_certificate /etc/httpd/ssl/example/example.crt; # full path to SSL certificate and CA certificate concatenated together
ssl_certificate_key /etc/httpd/ssl/example/example.key; # full path to SSL key
# performance enhancement for SSL
ssl_stapling off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
# safety enhancement to SSL: make sure we actually use a safe cipher
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:ECDHE-RSA-RC4-SHA:ECDHE-ECDSA-RC4-SHA:RC4-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!3DES:!MD5:!PSK';
# config to enable HSTS(HTTP Strict Transport Security) https://developer.mozilla.org/en-US/docs/Security/HTTP_Strict_Transport_Security
# to avoid ssl stripping https://en.wikipedia.org/wiki/SSL_stripping#SSL_stripping
add_header Strict-Transport-Security "max-age=31536000;";
# If your application is not compatible with IE <= 10, this will redirect visitors to a page advising a browser update
# This works because IE 11 does not present itself as MSIE anymore
#if ($http_user_agent ~ "MSIE" ) {
# return 303 https://browser-update.org/update.html;
#}
# pass all requests to Meteor
location / {
proxy_pass http://127.0.0.1:4001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade; # allow websockets
proxy_set_header Connection $connection_upgrade;
proxy_set_header X-Forwarded-For $remote_addr; # preserve client IP
# this setting allows the browser to cache the application in a way compatible with Meteor
45,1 Top
I'm running the meteor server on 4001 port.
Ok, this works perfectly for meteor. My problem now is that I want to create a vhost for a subdomain, payment.example.com, which will load larval 5.
Normally I want this subdomain to load over HTTPS also.
How on earth can I do this without loading the default nginx vhost settings or the domain vhost which loads meteor?
I can't figure out a way to make it work. This is the payment subdomain virtual host I'm trying to make and it's not working:
server {
listen 431 ssl spdy; # we enable SPDY here
listen [::]:431 ipv6only=on default_server;
server_name payment.example.ro; # this domain must match Common Name (CN) in the SSL certificate
root /var/www/html/example/payment/;
access_log /var/log/nginx/nginx_access.log;
error_log /var/log/nginx/nginx_error.log;
ssl_certificate /etc/httpd/ssl/example/example.crt; # full path to SSL certificate and CA certificate concatenated together
ssl_certificate_key /etc/httpd/ssl/example/example.key; # full path to SSL key
# performance enhancement for SSL
ssl_stapling off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
# safety enhancement to SSL: make sure we actually use a safe cipher
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:ECDHE-RSA-RC4-SHA:ECDHE-ECDSA-RC4-SHA:RC4-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!3DES:!MD5:!PSK';
# config to enable HSTS(HTTP Strict Transport Security) https://developer.mozilla.org/en-US/docs/Security/HTTP_Strict_Transport_Security
# to avoid ssl stripping https://en.wikipedia.org/wiki/SSL_stripping#SSL_stripping
add_header Strict-Transport-Security "max-age=31536000;";
location / {
root /var/www/html/example/payment/;
index index.php index.html index.htm;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Now when I load payment.example.com I load the meteor reversed proxy.
Any suggestions or ideas on what to do to make this work. Is this even possible?
Do I have to put it in a subfolder rather that subdomain? I prefer the subdomain...
Thank you guy!

nginx rewriting with drupal windows

this is the first time i use nginx , and i have two problems with it,
the first one is that i want to redirect http://localhost/project automatically to http://localhot/project/en/ and en is subdirectory on project.
The second problem : in localhost/project/en/ i can see the index.php i mean the main page but every redirecting from the project give me 404 not found like localhost/project/en/people/ or
localhost/project/en/people/article1
NB : i use nginx in windows with drupal and . here is my configuration:
worker_processes 1;
events {
worker_connections 1024;
}
http {
server_tokens on;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
ssi off;
#Timeouts
client_body_timeout 5;
client_header_timeout 5;
keepalive_timeout 25 25;
send_timeout 15s;
resolver_timeout 3s;
#Directive sets timeout period for connection with FastCGI-server. It should be noted that this value can't exceed 75 seconds.
fastcgi_connect_timeout 5s;
#Directive sets the amount of time for upstream to wait for a fastcgi process to send data. Change this directive if you have long running fastcgi processes that do not produce output until they have finished processing. If you are seeing an upstream timed out error in the error log, then increase this parameter to something more appropriate.
fastcgi_read_timeout 400s;
#Directive specifies request timeout to the server. The timeout is calculated between two write operations, not for the whole request. If no data have been written during this period then serve closes the connection.
fastcgi_send_timeout 150s;
fastcgi_buffers 8 32k;
fastcgi_buffer_size 32k;
#fastcgi_busy_buffers_size 256k;
#fastcgi_temp_file_write_size 256k;
open_file_cache off;
#php max upload limit cannot be larger than this
client_max_body_size 8m;
####client_body_buffer_size 1K;
client_header_buffer_size 1k;
large_client_header_buffers 2 1k;
types_hash_max_size 2048;
include nginx.mimetypes.conf;
default_type text/html;
##
# Logging Settings
##
access_log "c:/wt-nmp/log/nginx_access.log";
error_log "c:/wt-nmp/log/nginx_error.log" warn; #debug or warn
log_not_found on; #enables or disables messages in error_log about files not found on disk.
rewrite_log off;
#Leave this off
fastcgi_intercept_errors off;
gzip off;
index index.php index.htm index.html;
server {
listen 127.0.0.1:80 default_server;
#listen [::1]:80 ipv6only=on;
server_name localhost;
root "c:/wt-nmp/www/";
autoindex on;
allow 127.0.0.1;
#allow ::1;
deny all;
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
# deny all;
}
location /project {
index index.php;
try_files $uri $uri/ #handler;
}
location #handler { rewrite / /project/en/index.php; }
location ~ ^/en/ {
root "c:/wt-nmp/www/project";
try_files $uri $uri/ /en/index.php?$args;
}
#tools are now served from wt-nmp/include/tools/
location ~ ^/tools/.*\.php$ {
root "c:/wt-nmp/include";
try_files $uri =404;
include nginx.fastcgi.conf;
fastcgi_pass php_farm;
}
location ~ ^/tools/ {
root "c:/wt-nmp/include";
}
location ~ \.php$ {
# try_files $uri =404;
# fastcgi_pass php_farm;
fastcgi_pass 127.0.0.1:9000;
include nginx.fastcgi.conf;
}
}
include domains.d/*.conf;
include nginx.phpfarm.conf;
}
WPN-XM Server Stack
Installation Instructions for Drupal 7
with "Clean URLs"
Installation Steps:
Download http://ftp.drupal.org/files/projects/drupal-7.34.zip
Extract into c:\wpn-xm\www
rename versionized folder to just "drupal" = full path to drupal = c:\wpn-xm\www\drupal
run drupal install - http://localhost/drupal/install.php
activate missing PHP extensions: maybe gd2, mbstring, then restart php
reload install page, all green, proceed to database dialog
before filling the database dialog, create database "drupal" in adminer, then use that as db in dialog
proceed with install steps until done
Browse: http://localhost/drupal/
Ok, you just installed Drupal on localhost.
URL Rewriting and Clean URLs Steps:
After the installation, 3 things are needed to get short URLs working:
provide new host "http://drupal.dev"
add a Nginx Configuration for URL rewriting
enabling "Clean URLs" in the Drupal Configuration
Important Notice
The short/nice/clean URLs work only for URLs starting with
"http://drupal.dev/" not for "localhost". Using megaphone: not working
with "http://localhost/..." - use "http://drupal.dev/...".
Add "drupal.dev" to hosts file
You need to add "drupal.dev" to your "hosts" file.
manually or
via the WPN-XM Server Control Panel - Steps:
right-click tray icon - "Manage Hosts"
"Add" - Data "127.0.01" "drupal.dev"
Click Ok.
The Windows Permissions Dialog pops up.
Click Ok, to allow writing to the "hosts" file.
If Anti-Virus tool blocks writing to the hosts file, disable AV it, repeat the steps, enable it again.
Check: http://drupal.dev/?q=admin - ok
-
**Add a Nginx server block for Drupal7 **
Use the following Nginx config file to activate rewriting URLs:
https://github.com/WPN-XM/software/blob/master/nginx/config/conf/domains-disabled/drupal7.conf
You might use an include directive in your main nginx.conf to load it.
Restart or rehash Nginx to activate the new configuration.
You can now start using the URL "http://drupal.dev" in your browser.
Important is the the following directive in your server block
# Make site accessible from http://drupal.dev/
server_name drupal.dev;
and the rewrite rule rewrite ^/(.*)$ /index.php?q=$1;.
Check: http://drupal.dev/admin
Activate "Clean URLs" in Drupal Configuration
Testing "Clean URLs"
http://drupal.dev/admin/config/search/clean-urls/check
Response: {"status":true}
Enabling "Clean URLs" (b)
http://drupal.dev/admin/config/search/clean-urls
Check Checkbox & Save.
Links in the menu should appear in "Clean URL" form without "?q=".
Final Checks:
Hover links in the menu and check for short URLs. - ok
http://drupal.dev/admin - ok
http://drupal.dev/user/login - ok
Done.

Configuring nginx server to handle requests from multiple domains

Use Case:- I am working on a web application which allows to create HTML templates and publish them on amazon S3.Now to publish the websites I use nginx as a proxy server.
What the proxy server does is,when a user enters the website URL,I want to identify how to check if the request comes from my application i.e app.mysite.com(This won't change) and route it to apache for regular access,if its coming from some other domain like a regular URL www.mysite.com(This needs to be handled dynamically.Can be random) it goes to the S3 bucket that hosts the template.
My current configuration is:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
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;
charset utf-8;
keepalive_timeout 65;
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay off;
Default Server Block to catch undefined host names
server {
listen 80;
server_name app.mysite.com;
access_log off;
error_log off;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
}
}
}
Load all the sites
include /etc/nginx/conf.d/*.conf;
Updates as I was not clear enough :-
My question is how can I handle both the domains in the config file.My nginx is a proxy server on port 80 on an EC2 instance.This also hosts my application that runs on apache on a differnet port.So any request coming for my application will come from a domain app.mysite.com and I also want to proxy the hosted templates on S3 which are inside a bucket say sites.mysite.com/coolsite.com/index.html.So if someone hits coolsite.com I want to proxy it to the folder sites.mysite.com/coolsite.com/index.html and not to app.syartee.com.Hope I am clear
The other server block:
# Server for S3
server {
# Listen on port 80 for all IPs associated with your machine
listen 80;
# Catch all other server names
server_name _; //I want it to handle other domains then app.mysite.com
# This code gets the host without www. in front and places it inside
# the $host_without_www variable
# If someone requests www.coolsite.com, then $host_without_www will have the value coolsite.com
set $host_without_www $host;
if ($host ~* www\.(.*)) {
set $host_without_www $1;
}
location / {
# This code rewrites the original request, and adds the host without www in front
# E.g. if someone requests
# /directory/file.ext?param=value
# from the coolsite.com site the request is rewritten to
# /coolsite.com/directory/file.ext?param=value
set $foo 'http://sites.mysite.com';
# echo "$foo";
rewrite ^(.*)$ $foo/$host_without_www$1 break;
# The rewritten request is passed to S3
proxy_pass http://sites.mysite.com;
include /etc/nginx/proxy_params;
}
}
Also I understand I will have to make the DNS changes in the cname of the domain.I guess I will have to add app.mysite.com under the CNAME of the template domain name?Please correct if wrong.
Thank you for your time
Found this as a part of documentation but took a while to understand.
I had to add a default_server attribute in the second server block for rest of the domains to work.
If we config the server_name, nginx will serve the content based on config block that match the domain.
app.mysite.com
server {
listen 80;
server_name app.mysite.com;
# config for app.mysite.com
}
other websites
server {
listen 80 default_server; #To handle domains apart from the fixed domain(In my case app.mysite.com)
server_name _ ;
# config for coolsite.com, anotherdomain.com ...
}
I think you need to remove $foo from
rewrite ^(.*)$ $foo/$host_without_www$1 break;
With the foo you would pass
http://sites.mysite.com/coolsite.com/directory/file.ext?param=value
to
proxy_pass http://sites.mysite.com;
Just a guess

Categories