Nginx SSL inside a docker container - nginx

I have configured a Docker container to run Nginx and setup the /etc/nginx/sites-available/default file as shown below
server
{
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php index.html index.htm;
server_name example.com;
location / {
try_files $uri $uri/ /index.html;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server
{
listen 443;
root /usr/share/nginx/html;
index index.php index.html index.htm;
server_name example.com;
ssl on;
ssl_certificate /etc/ssl/certs/ssl-cert-snakeoil.pem;
ssl_certificate_key /etc/ssl/private/ssl-cert-snakeoil.key;
location / {
try_files $uri $uri/ /index.html;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I map the /etc/ssl/certs & /etc/ssl/private folders on the host when I run the docker container
docker run -dt -p 8080:443 -p 8081:80 -v /t-base/log:/var/log/nginx -v
/etc/ssl/certs:/etc/ssl/certs -v /etc/ssl/private:/etc/ssl/private nginx
Docker ps shows
Up n minutes 0.0.0.0:8081->80/tcp 0.0.0.0:8080->443/tcp <container-name>
and the nginx error log file inside the mapped /t-base/log folder stays empty.
docker exec -it <container-name> /bin/bash
followed by
service nginx status
just comes back and says that nginx is running.
All of the above would indicate that everything is working correctly. However, I find that whilst I am able to browse to
http://example.com:8080
turns up the default page
https://example.com:8081
has the Chrome showing me its default "sad smiley" error page. I cannot see what I might be doing wrong here. I'd much appreciate any help.

You have interchanged the ports. According to this command line -p 8080:443 -p 8081:80, you should do:
https://example.com:8080 note this is https
and
http://example.com:8081
This should work

Related

nginx configuration for multiple sites one ip address and one server

nginx multiple websites on same port and one ip address
for example my ip 192.167.10.2
and we want to run site on same ip
nginx/html contain two project like below
1: project1
2: project2
we run default project1 on ip :192.167.10.2
and second project run like 192.167.10.2/project2
how do configuration in nginx config file
Here is a simple example.You could try
First config :
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/mywebsite1;
index index.php index.html index.htm;
server_name mywebsite1.de www.mywebsite1.de;
location / {
try_files $uri/ /index.php?$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/mywebsite1;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Second config:
server {
listen 80;
listen [::]:80 //#here Be carefull , only one default
root /var/www/mywebsite2;
index index.php index.html index.htm;
server_name mywebsite1.de www.mywebsite1.de;
location / {
try_files $uri/ /index.php?$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/mywebsite1;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Don't forget to restart server.

Whats the correct way to write nginx configuration for wordpress?

I have a problem with configuring my wordpress nginx settings.
This is my nginx configuraton
server {
listen 80;
server_name wordpress.project;
access_log /var/log/nginx/wordpress.project/access.log;
error_log /var/log/nginx/wordpress.project/error.log;
root /var/www/wordpress.project;
location / {
index app_dev.php;
if (-f $request_filename) {
break;
}
rewrite ^(.*)$ /app_dev.php last;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
Hosts file is set as 127.0.0.1 wordpress.project
Permissions of folders are set
sudo chmod -R a+rw /var/www/wordpress.project
sudo chown -R www-data:www-data /var/www/wordpress.project
And symbolic link is set
sudo ln -s /etc/nginx/sites-available/wordpess.project /etc/nginx/sites-enabled/wordpess.project
And there is still an error when i try accessing wordpress.project saying
File not found
Project has classic wordpress structure of files
Is this correct configuration for wordpress in nginx and if its not what is the best way?
Try this
server
{
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/wordpress.project;
index index.php index.html index.htm;
server_name your_domain.com;
location / {
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
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;
}
}
Please restart Nginx after changing the configuration (sudo service nginx restart)

nginx - DNS works for www. but not for http://

I have a server that is using ngnix and I have a configuration file:
server {
listen 80;
server_name http://mycoolwebsite.com/;
return 301 http://www.mycoolwebsite.com$request_uri;
}
server {
listen 80;
root /var/www/website/front/public;
index index.php index.html index.htm;
server_name www.mycoolwebsite.com;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Now when I enter http://www.mycoolwebsite.com the website shows just fine. Whenever I enter http://mycoolwebsite.com I get:
This site can’t be reached | server DNS address could not be found.
What could be the issue here?
Your server name in the first server block should be:
server_name mycoolwebsite.com;
Also, make sure that there is a DNS entry for mycoolwebsite.com that points to your servers IP. This may be separate to the entry for www.mycoolwebsite.com.

Laravel5 and PhpMyadmin on Digital Ocean LEMP stack

I tried to install phpMyadmin on my LEMP server using following tutorial
https://www.digitalocean.com/community/tutorials/how-to-install-phpmyadmin-on-a-lemp-server
but when I try to access myip/phpmyadmin I get page cannot be found
before this I installed laravel5 so anything i am typing after ip/anystring its being accessed by my laravel route page ,
So can you tell me how I can access the phpMyadmin
my ip is http://xxxxxxxx//phpmyadmin .
and my default page for nginx looks like this
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/laravel/public;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name xxxxxxx;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
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;
}
}
Please hlp me out
Thanks
I am also stuck in the same kind of situation. Paste this code before location ~ .php$ function and it's work perfectly fine.
Source : Laravel routes overwriting phpmyadmin path with nginx
location /phpmyadmin {
root /usr/share/nginx/html;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/nginx/html;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/nginx/html;
}
}

NGINX url rewrite on digitalocean

I have problem with url rewrite from example.com to www.example.com on nginx web server. I using new hosting digitalocean.com and still struggling with this...
I will be happy with every opinion.
There is my code:
server {
server_name example.com;
return 301 http://www.example.com$request_uri;
}
server {
server_name www.example.com;
root /usr/share/nginx/www;
index index.php index.html index.htm;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
location / {
try_files $uri $uri/ /index.html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I have error when I trying to restart nginx server:
Restarting nginx: nginx: [emerg] could not build the server_names_hash, you should
increase server_names_hash_bucket_size: 32
nginx: configuration file /etc/nginx/nginx.conf test failed
The server name is probably too long for the default values.
Modify the /etc/nginx/nginx.conf file by adding the following below the http:
increase server_names_hash_bucket_size: 64
Save this value and test using the -t
nginx -t
Also, watch for any saved default configurations in the sites-available folder which may cause issues.
Try the following:
server {
server_name example.com;
return 301 http://www.example.com$request_uri;
}
server {
server_name www.example.com;
root /usr/share/nginx/www;
index index.php index.html index.htm;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
location / {
try_files $uri $uri/ /index.html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Resources