I'm learning NGINX and I need to set a frontend webserver with NGINX and a backend webserver with Apache using .htaccess
This is the content of /etc/nginx/sites-available/my_test5.loc
server {
charset utf-8;
client_max_body_size 128M;
listen 80;
server_name my_test5.loc;
root /var/www/my_test5.loc/web;
#root /var/www/my_test5.loc;
index index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
# deny accessing php files for the /assets directory
location ~ ^/assets/.*\.php$ {
deny all;
}
location ~ \.php$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
location ~* /\. {
# deny all;
allow all;
}
}
This is the content of /etc/apache2/sites-available/my_test5.loc.conf
<VirtualHost *:8080>
<Directory /var/www/my_test5.loc/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ServerAdmin admin#my_test5.loc
ServerName my_test5.loc
ServerAlias www.my_test5.loc
DocumentRoot /var/www/my_test5.loc/web/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Now all I have working is the main page. If I try to navigate to site/about for example I can't. What have I done wrong and how to use these 2 web servers?
I think your setup is only pointing correctly into index.php because of your location / {} directive, try editing your to be more permissive about your Apache access changing this block to pass through it and adding specific block to other kind of files, like assets.
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js|ttf|ttc|otf|eot|woff|woff2)$ {
try_files $uri $uri/
}
This way NGINX serves your static files and Apache serves everything else.
For Nginx work
use .htaccess for apache
siteName.conf apache must be
siteName conf must be in nginx sites-available and sites-enable
be sure that you send request from 80 port to 8080(as in my case) and Apache are listening it
Related
I have this configuration in apache working fine:
<VirtualHost *:80>
ServerAdmin mail#mail.com
ServerName opi4
<Directory "/var/www/html">
Require all denied
</Directory>
RewriteEngine on
RewriteRule "^/my-project$" "/my-project/" [L,R=301]
RewriteRule "^/amule$" "/amule/" [L,R=301]
RewriteRule "^/transmission$" "/transmission/web/" [L,R=301]
ProxyPass /my-project http://my-project.opi4
ProxyPassReverse /my-project http://my-project.opi4
ProxyPass /amule http://amule.opi4:4711
ProxyPassReverse /amule http://amule.opi4:4711
ProxyPass /transmission http://transmission.opi4:9091/transmission
ProxyPassReverse /transmission http://transmission.opi4:9091/transmission
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel debug
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I writed this one to use it in nginx:
server {
listen 80;
server_name opi4;
location /amule {
rewrite ^/amule$ /amule/ permanent;
proxy_pass http://amule.opi4:4711;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /transmission {
rewrite ^/transmission$ /transmission/web/ permanent;
rewrite ^/transmission/$ /transmission/web/ permanent;
proxy_pass http://transmission.opi4:9091/transmission;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /my-project/ {
rewrite ^/my-project$ /my-project/ permanent;
proxy_pass http://my-project.opi4/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
/etc/hosts
127.0.0.1 amule.opi4
127.0.0.1 transmission.opi4
127.0.0.1 my-project.opi4
When I used with amule, transmission works fine, but no with my-project.
works: http://amule.opi4/
works: http://opi4/amule
works: http://transmission.opi4/
works: http://opi4/transmission
works: http://my-project.opi4/
NOT works: http://opi4/my-project it shows a "Welcome to nginx!" the same result as http://opi4/
some help will be apreciated, thank you
I thought that i have only apache on my server, but i see that nginx catches some query's before apache do...
my .htaccess file:
RewriteEngine On
RewriteRule ^.*$ index.php [NC,L]
But when i go to url like
https://site.domain/folder/another/folder/file.png
I get 404 error, instead of result of my index.php-script!
I tried to change my nginx-config by many ways so it was in vain.
There is it:
server {
server_name site.domain www.site.domain;
charset off;
index index.html index.php;
disable_symlinks if_not_owner from=$root_path;
include /etc/nginx/vhosts-includes/*.conf;
include /etc/nginx/vhosts-resources/site.domain/*.conf;
access_log /var/www/httpd-logs/site.domain.access.log;
error_log /var/www/httpd-logs/site.domain.error.log notice;
ssi on;
return 301 https://$host:443$request_uri;
set $root_path /var/www/www-root/data/www/site.domain;
root $root_path;
location / {
location ~ [^/]\.ph(p\d*|tml)$ {
try_files /does_not_exists #fallback;
}
location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
try_files $uri $uri/ #fallback;
}
location / {
try_files /does_not_exists #fallback;
}
}
location #fallback {
proxy_pass http://127.0.0.1:8080;
proxy_redirect http://127.0.0.1:8080 /;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
access_log off;
}
listen 77.777.7.777:80;
}
server {
server_name ege2018.top www.site.domain;
ssl_certificate "/var/www/httpd-cert/www-root/site.domain_le1.crtca";
ssl_certificate_key "/var/www/httpd-cert/www-root/site.domain_le1.key";
ssl_ciphers EECDH:+AES256:-3DES:RSA+AES:!NULL:!RC4;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_dhparam /etc/ssl/certs/dhparam4096.pem;
charset off;
index index.html index.php;
disable_symlinks if_not_owner from=$root_path;
include /etc/nginx/vhosts-includes/*.conf;
include /etc/nginx/vhosts-resources/site.domain/*.conf;
access_log /var/www/httpd-logs/site.domain.access.log;
error_log /var/www/httpd-logs/site.domain.error.log notice;
ssi on;
set $root_path /var/www/www-root/data/www/site.domain;
root $root_path;
location / {
location ~ [^/]\.ph(p\d*|tml)$ {
try_files /does_not_exists #fallback;
}
location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
try_files $uri $uri/ #fallback;
}
location / {
try_files /does_not_exists #fallback;
}
}
location #fallback {
proxy_pass http://127.0.0.1:8080;
proxy_redirect http://127.0.0.1:8080 /;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
access_log off;
}
listen 77.777.7.777:443 ssl;
}
(I changed-to-hide domain name and server ip)
Please, help me to solve this problem, i hope it's possible just by editing nginx-config.
Thank you!
Have you got access to both nginx and Apache HTTPd logs? tail -F both log files while you access the URL. #fallback does indeed look like it sends the request to HTTPd if it cannot find it locally, does the request get to the HTTPd log? You may find that index.php isn't where HTTPd is expecting to find it.
My bad! I changed-to-hide actually url, which i want to visit, but key was there.
The url was like
site.domain/icons/...
And apache go to his own icons folder despite .htaccess instructions...
I touched apache config to solve this problem - commented alias for /icons/
I am trying to insert a new website in a digital ocean server. There are currently 4 websites being hosted there in Nginx. I've created a Nginx Virtual Host that works as a reverse proxy for Apache, which is listening in the port 8000. It is currently redirecting when I try to access the domain, but it is failing to load all the CSS and Javascript. It just loads the HTML. But if I access it through www.domain.com:8000 it loads perfectly.
The Nginx configuration file for the website:
server {
listen 80;
root /usr/share/nginx/html/;
index index.php index.html index.htm;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ /index.html?$args;
# include /etc/nginx/mime.types;
}
# location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
# expires 1s;
# }
location ~ \.css {
add_header Content-Type text/css;
}
location ~ \.js {
add_header Content-Type application/x-javascript;
}
location ~ \.html$ {
include /etc/nginx/mime.types;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://example.com:8000;
}
location ~ /\.ht {
deny all;
}
}
The Apache .conf file:
Listen 8000
<VirtualHost *:8000>
ServerAdmin admin#gmail.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I don't use PHP on the website, just HTML, CSS and JavaScript.
I have a Amazon EC2 Server setup with Nginx 1.8.1 on port 80 and Apache 2.4 on port 8080.
My Nginx error log is filled with invalid local address "www.domain.com:80"
Can someone advise how to fix this error?
My domain.com under /etc/nginx/sites-enabled is as below
server {
listen 80;
server_name domain.com www.domain.com;
root /home/domain/public_html/;
index index.php index.htm index.html;
location / {
#try_files $uri $uri/ /index.php;
#try_files $uri $uri/ $uri.php;
try_files $uri $uri/ /index.php?/$request_uri;
}
location ~ \.php$ {
proxy_bind $host:80;
proxy_pass http://www.domain.com:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
add_header X-Cache-Status $upstream_cache_status;
# Cache configuration
proxy_cache my-cache;
proxy_cache_valid 10s;
proxy_no_cache $cookie_PHPSESSID;
proxy_cache_bypass $cookie_PHPSESSID;
proxy_cache_key "$scheme$host$request_uri";
}
# Disable Cache for the file type html, json
location ~* .(?:manifest|appcache|html?|xml|json)$ {
expires -1;
}
# Enable Cache the file 30 days
location ~* .(jpg|png|gif|jpeg|css|mp3|wav|swf|mov|doc|pdf|xls|ppt|docx|pptx|xlsx)$ {
proxy_cache_valid 200 120m;
expires 30d;
proxy_cache my-cache;
#access_log off;
add_header X-Cache-Status $upstream_cache_status;
}
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
}
My domain.com under Apache Virtual Host is as below
<VirtualHost 172.31.xx.xxx:8080>
DocumentRoot /home/domain/public_html/
ServerName domaine.com
ServerAlias www.domain.com n1.domain.com
ErrorLog /home/domain/logs/error_log
CustomLog /home/domain/logs/access_log combined
<Directory /home/domain/public_html>
Options -Includes -ExecCGI
AllowOverride All
</Directory>
Is there any particular reason as to why you have the proxy_bind $host:80; directive? If not, remove it and it should stop the error (note that the address that you're binding to must be local, if you chose to go that route).
ok
now i installed laravel 4.2 on ubuntu 14.4 with apache and nginx proxy .
and when i create route like
Route::get('/reg', function()
{
return 'hii';
});
apache view "hii" when i call "localhost:8080/reg"
but nginx view main page when i call "localhost/reg" in browser
the nginx config :
server {
listen 80;
root /var/www/laravel/public;
index index.php index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
location ~ \.php$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
# PHP FPM configuration.
location ~* \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script$
}
location ~ /\.ht {
deny all;
}
}
where is wrong ?
According to your nginx.conf, It's loading your /var/www/laravel/public/index.php;. If you don't want that to happen,
adjust the path in nginx.conf to the file you want to load,
OR
make a view, and then point the path to that view.